2017-10-27 1 views
-1

큰 JSON 파일에 대해 사과드립니다. 각 링크 (embedLink 아래), 태그 및 순위를 추출하는 CSV 파일을 만들 수 있기를 원합니다. 그러나, 나의 시도는 각각 짧아졌다. 이것은 현재 가지고있는 코드입니다. 그러나 여러 개의 오류를 출력합니다.Python으로 JSON 파일을 CSV 파일로 변환하는 방법은 무엇입니까?

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 

나는, 내가 부족 모르겠어요 나는 오랫동안이 작업을 봤는데 그것은 나를 유세 됐어요. 내 문제에 대한 가능한 해결책이 있다면 알려주십시오. 나는 모든 아이디어에 열려 있습니다.

+0

먼저 Dataframe에서 나중에 .json 변환 할 수 있습니다. – wallyk

+0

0, 댄스, 바보, 칼튼, 은행, 신선한 왕자, http://media.giphy.com/media/MazO89pt5NljG/giphy.gif 1, excitedexcited, jeremy renner, http://media1.giphy.com/media /HscrwGNPHio2A/giphy.gif 2, tv, 행복, 30 락, 티나 페이, 하이 파이브, 리즈 레몬, 축하, http://media2.giphy.com/media/CCJnMBqEYxxEk/giphy.gif –

+0

죄송합니다, 링크 종류 그것을 망 쳤어. 숫자 (0, 1, 2)는 별도의 줄에 있어야합니다. –

답변

1

당신은 출력 CSV가 어떻게 보일지의 예를 추가하십시오 팬더를 사용하고 .CSV

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

팬더는 한번도 사용 해본 적이 없지만 한번만 알려 드리겠습니다. 감사! –

+0

@ P.Nar Pandas는 현재 Python으로 작성된 라이브러리이며 데이터 분석 작업을위한 매우 강력한 도구이며 xls, csv, json html, fwf 등으로 형식을 변경하는 목적에 매우 잘 부합 할 수 있습니다. 코드가 완료되면 이 답변에 표시를하고 확인 표시를하십시오. 감사 –

관련 문제