Python:如何从词典列表中创建csv字符串(无文件)?

时间:2019-10-08 09:12:21

标签: python string list csv dictionary

在Python中,我有一个这样的词典列表:

[
    {
        "col2": "2",
        "id": "1",
        "col3": "3",
        "col1": "1"
    },
    {
        "col2": "4",
        "id": "2",
        "col3": "6",
        "col1": "2"
    },
    {
        "col1": "1",
        "col2": "4",
        "id": "3",
        "col3": "7"
    }
]

,我需要将其转换为csv格式的字符串,包括标题行。 (对于初学者,我们不必关心列和行分隔符...) 因此,理想的结果是:

id,col1,col2,col3
1,1,2,3
2,2,4,6
3,1,4,7

(“理想情况”是因为列顺序并不重要;尽管先拥有“ id”列会很好...)

我已经搜索过SOF,并且有很多类似的问题,但是答案总是涉及使用csv.DictWriter创建一个csv 文件。我不想创建文件,我只想要那个字符串!

当然,我可以遍历列表,并在此循环中遍历字典键,并以此方式使用字符串操作创建csv字符串。但是肯定有一些更优雅,更有效的方法可以做到这一点吗?

此外,我知道Pandas库,但是我试图在非常有限的环境中执行此操作,在该环境中,我只想使用内置模块。

4 个答案:

答案 0 :(得分:3)

最简单的方法是使用熊猫:

import pandas as pd
df = pd.DataFrame.from_dict(your_list_of_dicts)
print(df.to_csv(index=False))

结果:

col1,col2,col3,id
1,2,3,1
2,4,6,2
1,4,7,3

如果要对列进行重新排序,没有什么比这容易的了:

col_order = ['id', 'col1', 'col2', 'col3']
df[col_order].to_csv(index=False)

或者,仅确保第一列id

df.set_index('id', inplace=True) # the index is always printed first
df.to_csv() # leave the index to True this time

答案 1 :(得分:2)

具有内置功能:

from collections import OrderedDict

ord_d = OrderedDict().fromkeys(('id', 'col1', 'col2', 'col3'))
s = ','.join(ord_d.keys()) + '\n'
for d in lst:
    ord_d.update(d)
    s += ','.join(ord_d.values()) + '\n'

print(s)

输出:

id,col1,col2,col3
1,1,2,3
2,2,4,6
3,1,4,7

答案 2 :(得分:0)

这个想法是获取所有可能的键并获取所有值。 假设数据是您拥有的字典列表。 这应该起作用:

output = ''
all_keys = set().union(*(d.keys() for d in data))
output += ",".split(all_keys) + '\n'
for item in data:
    item_str = ",".split([data[key] for key in all_keys if key in data else ''])
    output += item_str + '\n'

source

答案 3 :(得分:0)

您可以使用io.StringIO而不是文件来写入“字符串”。以csv.DictWriter为例,我们得到以下代码:

import csv
import io

data = [...]  # your list of dicts

with io.StringIO() as csvfile:
    fieldnames = ['id', 'col1', 'col2', 'col3']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    for row in data:
        writer.writerow(row)
    print(csvfile.getvalue())
相关问题