繁体   English   中英

如何在 python 中将 JSON 文件转换为 CSV 文件?

[英]How do I convert a JSON file to a CSV file in python?

我为大型 JSON 文件道歉。 我希望能够创建一个 csv 文件来提取每个链接(在 embedLink 下)、标签和排名。 然而,我的每一次尝试都失败了。 这是我目前拥有的代码; 但是,它输出多个错误。

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

import json, csv

x="""{u'results': [{u'index': 4367, u'content_data': {u'embedLink': 
u'http://media.giphy.com/media/MazO89pt5NljG/giphy.gif', u'tags': [u'dance', 
u'silly', u'carlton', u'banks', u'fresh prince'], u'added_with_admin': True, 
u'dateAdded': 1394519920.03, u'still_image': 
u'http://media.giphy.com/media/MazO89pt5NljG/200_s.gif'}, u'parameters': 
{u'mu': 38.367645789902205, u'sigma': 2.184739493694442}, u'cID': 
u'f10e5808d396e2e4ba0ab48c', u'rank': 0, u'content': u'MazO89pt5NljG', 
u'content_type': u'gif'}, {u'index': 370, u'content_data': {u'embedLink': 
u'http://media1.giphy.com/media/HscrwGNPHio2A/giphy.gif', u'tags': 
[u'excitedexcited', u'jeremy renner'], u'added_with_admin': False, 
u'dateAdded': 1393863494.644655, u'still_image': 
u'http://media.giphy.com/media/HscrwGNPHio2A/200_s.gif'}, u'parameters': 
{u'mu': 37.24778379219212, u'sigma': 1.5847716442668915}, u'cID': 
u'5314ab46d34b6c5b402aeb39', u'rank': 1, u'content': u'HscrwGNPHio2A', 
u'content_type': u'gif'}, {u'index': 287, u'content_data': {u'embedLink': 
u'http://media2.giphy.com/media/CCJnMBqEYxxEk/giphy.gif', u'tags': [u'tv', 
u'happy', u'30 rock', u'tina fey', u'high five', u'liz lemon', 
u'celebration'], u'added_with_admin': False, u'dateAdded': 
1393863490.821621, u'still_image': 
u'http://media.giphy.com/media/CCJnMBqEYxxEk/200_s.gif'}, u'parameters': 
{u'mu': 36.59521670234331, u'sigma': 1.681281739841269}, u'cID': 
u'5314ab42d34b6c5b402aeae6', u'rank': 2, u'content': u'CCJnMBqEYxxEk', 
u'content_type': u'gif'}, {u'index': 2681, u'content_data': {u'embedLink': 
u'http://media.giphy.com/media/Mn0C1r0qL7XWg/giphy.gif', u'tags': [u'funny', 
u'happy', u'excited', u'crazy'], u'added_with_admin': True, u'dateAdded': 
1394378341.692, u'still_image': u 
u'http://media.giphy.com/media/Mn0C1r0qL7XWg/200_s.gif'}, u'parameters': 
{u'mu': 36.56837392887614, u'sigma': 1.6136408794819588}, u'cID': 
u'3844637f65451dffd3698425', u'rank': 3, u'content': u'Mn0C1r0qL7XWg', 
u'content_type': u'gif'}, {u'index': 247, u'content_data': {u'embedLink': 
u'http://media.giphy.com/media/AAXjZcPFzTPO/giphy.gif', u'tags': [u'smile', 
u'happy', u'laughing', u'black', u'hipster', u'beautiful girls', u'be 
happy', u'pixie cut'], u'added_with_admin': False, u'dateAdded': 
1393863489.035277, u'still_image': 
u'http://media.giphy.com/media/AAXjZcPFzTPO/200_s.gif'}, u'parameters': 
{u'mu': 36.34056334299456, u'sigma': 1.7996651935876014}, u'cID': 
u'5314ab41d34b6c5b402aeabe', u'rank': 4, u'content': u'AAXjZcPFzTPO', 
u'content_type': u'gif'}, {u'index': 1429, u'content_data': {u'embedLink': 
u'http://media.giphy.com/media/7SQtrK5JbFTFK/giphy.gif', u'tags': 
[u'cartoons & comics', u'dancing', u'the simpsons', u'friday', u'lisa 
simpson', u'tgif'], u'added_with_admin': False, u'dateAdded': 
1394091573.355619, u'still_image': 
u'http://media.giphy.com/media/7SQtrK5JbFTFK/200_s.gif'}, u'parameters': 
{u'mu': 36.2797522468539, u'sigma': 1.3927800299100415}, u'cID': 
u'53182635d34b6c1910c00332', u'rank': 5, u'content': u'7SQtrK5JbFTFK', 
u'content_type': u'gif'}, {u'index': 2749, u'content_data': {u'embedLink': 
u'http://media2.giphy.com/media/wRGJgKQdpagFO/giphy.gif', u'tags': 
[u'dancing', u'happy', u'jimmy fallon', u'jimmy fallon'], 
u'added_with_admin': True, u'dateAdded': 1394386054.159, u'still_image': 
u'http://media.giphy.com/media/wRGJgKQdpagFO/200_s.gif'}, u'parameters': 
{u'mu': 36.14775637052213, u'sigma': 1.365824510294118}, u'cID': 
u'010e4981a9a97e6a007d8a65', u'rank': 6, u'content': u'wRGJgKQdpagFO', 
u'content_type': u'gif'}, {u'index': 12, u'content_data': {u'embedLink': u 
u'http://media.giphy.com/media/10UeedrT5MIfPG/giphy.gif', u'tags': 
[u'dancing', u'giphytrending', u'happy', u'cartoons', u'giphytv', u'tom and 
jerry'], u'added_with_admin': False, u'dateAdded': 1393863477.161998, 
u'still_image': u'http://media.giphy.com/media/10UeedrT5MIfPG/200_s.gif'}, 
u'parameters': {u'mu': 36.09150478133458, u'sigma': 1.4215081662482691}, 
u'cID': u'5314ab35d34b6c5b402ae9d3', u'rank': 7, u'content': 
u'10UeedrT5MIfPG', u'content_type': u'gif'}, {u'index': 1624, 
u'content_data': {u'embedLink': 
u'http://media.giphy.com/media/h8UyZ6FiT0ptC/giphy.gif', u'tags': 
[u'dancing', u'happy', u'birthday', u'fat', u't', u'giphybirthday', u'happy 
dance', u'taco bell', u'junk food', u'oh yea', u't card'], 
u'added_with_admin': False, u'dateAdded': 1394091596.424046, u'still_image': 
u'http://media.giphy.com/media/h8UyZ6FiT0ptC/200_s.gif'}, u'parameters': 
{u'mu': 35.96712787907767, u'sigma': 1.4742260856412648}, u'cID': 
u'5318264cd34b6c1910c003f5', u'rank': 8, u'content': u'h8UyZ6FiT0ptC', 
u'content_type': u'gif'}, {u'index': 193, u'content_data': {u'embedLink': 
u'http://media.giphy.com/media/6onMzNPjtFeCI/giphy.gif', u'tags': 
[u'laughing', u'excited', u'minions'], u'added_with_admin': False, 
u'dateAdded': 1393863486.381926, u'still_image': u 
u'http://media.giphy.com/media/6onMzNPjtFeCI/200_s.gif'}, u'parameters': 
{u'mu': 35.77139953100954, u'sigma': 1.3739202465218552}, u'cID': 
u'5314ab3ed34b6c5b402aea88', u'rank': 9, u'content': u'6onMzNPjtFeCI', 
u'content_type': u'gif'}], u'query_parameters': {u'sort': 1, u'skip': 0, 
u'limit': 10, u'mID': u'54a309ae1c61be23aba0da5c'}}
"""

x = json.loads(x)

f = csv.writer(open("test.csv", "w"))

f.writerow(["results", "index"])

for row in x:
   f.writerow( [row['results'], row['index']] )

示例所需的输出:

0, dance, silly, carlton, banks, fresh prince, media.giphy.com/media/MazO89pt5NljG/giphy.gif
1, excitedexcited, jeremy renner, media1.giphy.com/media/HscrwGNPHio2A/giphy.gif
2, tv, happy, 30 rock, tina fey, high five, liz lemon, celebration, media2.giphy.com/media/CCJnMBqEYxxEk/giphy.gif 

我不确定我错过了什么,我已经在这方面工作了很长时间,它一直困扰着我。 如果您对我的问题有任何可能的解决方案,请告诉我。 我对所有想法持开放态度。

您可以先在 Dataframe 中使用 pandas 并转换 .json,然后再在 .csv 中转换

import pandas as pd
df = pd.read_json('filename.json')
df.to_csv(filename.csv)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM