将python字典写入CSV,但仅包含特定列

时间:2018-11-16 13:15:41

标签: python dictionary export-to-csv

我有一个字典,其中有8列需要写入CSV,它可以正常工作,但是我只想将字典中的特定列转换为csv。怎么做

import flask.ext.sqlalchemy as flask_sqlalchemy

import flask_sqlalchemy as flask_sqlalchemy # Modified

2 个答案:

答案 0 :(得分:2)

import pandas as pd

dictionary = {'data': [{'channel_id': 'UCqTFe5Dz3mpixUrLfMmY6dw', 'title': 'Manoyek', 'channel_img': 'https://yt3.ggpht.com/a-/AN66SAx-JBOt1_NI6nXTBL2R95kDBaR6Spy9i4_q-g=s240-mo-c-c0xffffffff-rj-k-no', 'desc': 'Jestem Adrian' , 'subscriberCount_gained': 587372, 'subscriberCount_lost': 0}]}

data = pd.DataFrame.from_dict(dictionary["data"])

data_to_write = data[["channel_id", "channel_img", "desc"]]
data_to_write.to_csv("filename.csv")

答案 1 :(得分:0)

使用熊猫!

import pandas as pd

pd.DataFrame(dictionary).to_csv('filename.csv')