2016-11-07 24 views
0

ファイルに貼り付けられたJSONのストリーム(ほぼ10,000)を、ヘッダーと値の特定のフォーマットのCSVファイルに変換したいと考えています。その上JSONのストリームをCSVに変換する

{"shortUrlClicks":"594","longUrlClicks":"594","countries":[{"count":"125","id":"IQ"},{"count":"94","id":"US"},{"count":"56","id":"TR"},{"count":"50","id":"SA"},{"count":"29","id":"DE"},{"count":"24","id":"TN"},{"count":"20","id":"DZ"},{"count":"14","id":"EG"},{"count":"13","id":"MA"},{"count":"12","id":"PS"}],"browsers":[{"count":"350","id":"Chrome"},{"count":"100","id":"Firefox"},{"count":"46","id":"Safari"},{"count":"35","id":"Mobile"},{"count":"20","id":"Mobile Safari"},{"count":"20","id":"SamsungBrowser"},{"count":"8","id":"MSIE"},{"count":"6","id":"Opera"},{"count":"3","id":"OS;FBSV"},{"count":"2","id":"Maxthon"}],"platforms":[{"count":"227","id":"Android"},{"count":"221","id":"Windows"},{"count":"67","id":"iPhone"},{"count":"30","id":"X11"},{"count":"25","id":"Macintosh"},{"count":"8","id":"iPad"},{"count":"2","id":"Android 4.2.2"},{"count":"1","id":"Android 4.1.2"},{"count":"1","id":"Android 4.3"},{"count":"1","id":"Android 5.0.1"}],"referrers":[{"count":"340","id":"unknown"},{"count":"193","id":"t.co"},{"count":"38","id":"m.facebook.com"},{"count":"12","id":"addpost.it"},{"count":"4","id":"plus.google.com"},{"count":"3","id":"www.facebook.com"},{"count":"1","id":"goo.gl"},{"count":"1","id":"l.facebook.com"},{"count":"1","id":"lm.facebook.com"},{"count":"1","id":"plus.url.google.com"}]} 
{"shortUrlClicks":"594","longUrlClicks":"594","countries":[{"count":"125","id":"IQ"},{"count":"94","id":"US"},{"count":"56","id":"TR"},{"count":"50","id":"SA"},{"count":"29","id":"DE"},{"count":"24","id":"TN"},{"count":"20","id":"DZ"},{"count":"14","id":"EG"},{"count":"13","id":"MA"},{"count":"12","id":"PS"}],"browsers":[{"count":"350","id":"Chrome"},{"count":"100","id":"Firefox"},{"count":"46","id":"Safari"},{"count":"35","id":"Mobile"},{"count":"20","id":"Mobile Safari"},{"count":"20","id":"SamsungBrowser"},{"count":"8","id":"MSIE"},{"count":"6","id":"Opera"},{"count":"3","id":"OS;FBSV"},{"count":"2","id":"Maxthon"}],"platforms":[{"count":"227","id":"Android"},{"count":"221","id":"Windows"},{"count":"67","id":"iPhone"},{"count":"30","id":"X11"},{"count":"25","id":"Macintosh"},{"count":"8","id":"iPad"},{"count":"2","id":"Android 4.2.2"},{"count":"1","id":"Android 4.1.2"},{"count":"1","id":"Android 4.3"},{"count":"1","id":"Android 5.0.1"}],"referrers":[{"count":"340","id":"unknown"},{"count":"193","id":"t.co"},{"count":"38","id":"m.facebook.com"},{"count":"12","id":"addpost.it"},{"count":"4","id":"plus.google.com"},{"count":"3","id":"www.facebook.com"},{"count":"1","id":"goo.gl"},{"count":"1","id":"l.facebook.com"},{"count":"1","id":"lm.facebook.com"},{"count":"1","id":"plus.url.google.com"}]} 

...と:

は、私は次のようなJSONデータのストリームを持っています。

私は何でもヘッダ(shortUrlclicks、longUrclicks、など)があるとCSVにこの形式に変換したい:あなたは私を助けてくださいことができれば、私はに感謝するでしょう

OUTPUT

同じ。 Pythonやその他の言語のコードは役に立ちます。

答えて

0

Python標準ライブラリのJSONライブラリを使用して、OSライブラリ(Python標準ライブラリからも)を使用してJSONを読み取り、ファイルを読み書きすることができます。

それはこのようなものになるだろう:

f = File.open('file.json', 'r') 
items = json.loads(f.read()) 
csv_file = "" 

for row in items(): 
    new_row = "" 
    # get columns somehow 
    for column in columns: 
     new_row = "%s," % column 

    # Finished row, append a '\n' char to the row string. 
    csv_file.append("%s\n" % new_row) 

# write json file 
out = File.open('out.csv', 'rw') 
out.write(csv_file) 
out.close() 

PS:私は投稿する前にこのコードを実行されませんでした。これはアイデアを得るためのものです。

関連する問題