2017-10-27 15 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

に最初のデータフレームでは、後に.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、happy、30 rock、tina fey、high five、lizレモン、お祝い、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などのフォーマットに変更する目的にも非常に役立ちます。この回答に印をつけて確認してください。ありがとう –

関連する問題