简体   繁体   中英

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

I apologize for the large JSON file. I want to be able to create a csv file that extracts each link (under embedLink), the tags, and the rank. However, each of my attempts have come up short. This is the code that I currently have; however, it outputs multiple errors.

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']] )

Example desired output:

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 

I'm not sure what I'm missing, I've been working on this for a long time and it's been stumping me. If you have any possible solution to my problem, let me know. I'm open to all ideas.

You may use pandas and convert .json first in Dataframe and later in .csv

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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