当前位置: 首页>>代码示例>>Python>>正文


Python Table.to_dict方法代码示例

本文整理汇总了Python中outputty.Table.to_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Table.to_dict方法的具体用法?Python Table.to_dict怎么用?Python Table.to_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在outputty.Table的用法示例。


在下文中一共展示了Table.to_dict方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_to_dict_should_filter_some_columns

# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import to_dict [as 别名]
 def test_to_dict_should_filter_some_columns(self):
     table = Table(headers=["spam", "eggs", "ham"])
     table.append([42, 3.14, 2.71])
     table.append(["python", "rules", "yeh"])
     table_dict = table.to_dict(only=("eggs", "ham"))
     expected = {"eggs": [3.14, "rules"], "ham": [2.71, "yeh"]}
     self.assertEqual(table_dict, expected)
开发者ID:andreas-andrade,项目名称:outputty,代码行数:9,代码来源:test_Table_base.py

示例2: test_to_dict_should_filter_create_dict_from_values

# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import to_dict [as 别名]
 def test_to_dict_should_filter_create_dict_from_values(self):
     table = Table(headers=["spam", "eggs", "ham"])
     table.append([42, 3.14, 2.71])
     table.append(["python", "rules", "yeh"])
     table_dict = table.to_dict(key="spam", value="ham")
     expected = {42: 2.71, "python": "yeh"}
     self.assertEqual(table_dict, expected)
开发者ID:andreas-andrade,项目名称:outputty,代码行数:9,代码来源:test_Table_base.py

示例3: test_to_dict_should_create_a_dict_with_column_names_and_values

# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import to_dict [as 别名]
 def test_to_dict_should_create_a_dict_with_column_names_and_values(self):
     table = Table(headers=["spam", "eggs"])
     table.append([42, 3.14])
     table.append(["python", "rules"])
     table_dict = table.to_dict()
     expected = {"spam": [42, "python"], "eggs": [3.14, "rules"]}
     self.assertEqual(table_dict, expected)
开发者ID:andreas-andrade,项目名称:outputty,代码行数:9,代码来源:test_Table_base.py

示例4: test_to_dict_should_handle_encodings_correctly

# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import to_dict [as 别名]
    def test_to_dict_should_handle_encodings_correctly(self):
        table = Table(headers=["spam", "eggs", "ham"], input_encoding="iso-8859-1", output_encoding="utf16")
        table.append([42, 3.14, 2.71])
        table.append(["python", "rules", "yeh"])
        table.append([u"Álvaro".encode("iso-8859-1"), "...", "Justen"])
        table_dict = table.to_dict(key="spam", value="ham")
        expected = {
            42: 2.71,
            u"python".encode("utf16"): u"yeh".encode("utf16"),
            u"Álvaro".encode("utf16"): u"Justen".encode("utf16"),
        }
        self.assertEqual(table_dict, expected)

        table_dict_2 = table.to_dict()
        expected_2 = {
            u"spam".encode("utf16"): [42, u"python".encode("utf16"), u"Álvaro".encode("utf16")],
            u"eggs".encode("utf16"): [3.14, u"rules".encode("utf16"), u"...".encode("utf16")],
            u"ham".encode("utf16"): [2.71, u"yeh".encode("utf16"), u"Justen".encode("utf16")],
        }
        self.assertEqual(table_dict_2, expected_2)
开发者ID:andreas-andrade,项目名称:outputty,代码行数:22,代码来源:test_Table_base.py

示例5: Table

# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import to_dict [as 别名]
#!/usr/bin/env python
# coding: utf-8

#A `Table` made with `dict`-like, `list`-like and `tuple`-like objects. For
#example, this code:

from outputty import Table
my_table = Table(headers=['First Name', 'Last Name', 'Main Language'])
my_table.append({'First Name': 'Álvaro', 'Last Name': 'Justen',
                      'Main Language': 'Python'})
my_table.append(('Flávio', 'Amieiro', 'Python'))

rows = my_table.to_list_of_dicts()
print rows[1]['First Name']

table_dict = my_table.to_dict()
print table_dict

table_dict_filtered = my_table.to_dict(only=['First Name', 'Last Name'])
print table_dict_filtered

other_table = Table(headers=['date', 'measure'])
other_table.append(('2011-12-01', 21))
other_table.append(('2011-12-02', 42))
other_table.append(('2011-12-03', 3.14))
other_table.append(('2011-12-04', 2.71))
values_as_dict = other_table.to_dict(key='date', value='measure')
print values_as_dict
开发者ID:alextercete,项目名称:outputty,代码行数:30,代码来源:normalization.py


注:本文中的outputty.Table.to_dict方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。