【发布时间】:2019-02-07 20:55:51
【问题描述】:
我有大量从 twitter JSON 数据转换而来的字典。现在,我想把它们变成一个 .csv 文件。我搜索了该站点,但解决方案似乎适合具有很少值或已经存在的字典的字典。在我的情况下,键的数量要多一些,而且我还必须通过一个迭代过程将每个 JSON 文件转换为 dict。换句话说,我想在迭代过程中将每个 JSON 文件转换为 dict 文件后立即将它们写入 .csv 文件。
到目前为止,这是我的代码:
json_path = "C://Users//msalj//OneDrive//Desktop//pypr//Tweets"
for filename in os.listdir(json_path):
with open(filename, 'r') as infh:
for data in json_parse(infh):
这是我转换的 JSON 文件的示例:
{'actor': {'displayName': 'RIMarkable',
'favoritesCount': 0,
'followersCount': 0,
'friendsCount': 0,
'id': 'id:twitter.com:3847371',
'image': 'Picture_13.png',
'languages': ['en'],
'link': 'ht........ble',
'links': [{'href': 'htt.....m', 'rel': 'me'}],
'listedCount': 0,
'objectType': 'person',
'postedTime': '2007-01-09T02:53:35.000Z',
'preferredUsername': 'RIMarkable',
'statusesCount': 0,
'summary': 'The Official, Unofficial BlackBerry Weblog',
'twitterTimeZone': 'Eastern Time (US & Canada)',
'utcOffset': '0',
'verified': False},
'body': 'Jim Balsillie To Present At JP Morgan Technology Conference: Research in Motion co-CEO, Jim Balsillie,.. ht...qo',
'generator': {'displayName': 'twitterfeed', 'link': 'htt......om'},
'gnip': {'matching_rules': [{'tag': None, 'value': '"JP Morgan"'}]},
'id': 'tag:search.twitter.com,2005:66178882',
'link': 'ht...82',
'object': {'id': 'object:search.twitter.com,2005:66178882',
'link': 'ht.....82',
'objectType': 'note',
'postedTime': '2007-05-16T19:00:24.000Z',
'summary': 'Jim Balsillie To Present At JP Morgan Technology Conference: Research in Motion co-CEO, Jim Balsillie,.. ht......qo'},
'objectType': 'activity',
'postedTime': '2007-05-16T19:00:24.000Z',
'provider': {'displayName': 'Twitter',
'link': 'ht......m',
'objectType': 'service'},
'retweetCount': 0,
'twitter_entities': {'hashtags': [],
'urls': [{'expanded_url': None,
'indices': [105, 130],
'url': 'htt.......5qo'}],
'user_mentions': []},
'verb': 'post'}
有人可以帮我写代码吗?非常感谢!
【问题讨论】:
-
提供的 json 无效,因此很难编写示例代码。请验证它并将其发布在代码块中
-
我提供的不再是 JSON。我已将我的 JSON 文件转换为 dict 类型。
-
我尝试将其复制并粘贴到我的 ide 中,python 没有将其识别为字典或 json
-
您的问题取决于您希望在 CSV 文件中包含哪些信息。你想要dict中的哪些数据? CSV 文件中将包含哪些列?
标签: python json csv dictionary twitter