将数据从json文件打印到csv文件

时间:2013-03-17 09:23:24

标签: python csv python-3.x

当我将我的json文件中的数据打印到csv文件时,它不会在diff列中打印出来。 这是我的代码

import json

import urllib
import csv

def main():

  f1 = open('tweet-stream.json','r')
  Outputfile =open('newdata3.csv', 'w')

  count = 0

  for line in f1:
    d = json.loads(line)
    lang =  d["user"]["lang"]
    status_count = d["user"]["statuses_count"]
    print >>Outputfile,"Language: " + lang + "Status_Count" +str(status_count)


if __name__ == "__main__":
   main()

1 个答案:

答案 0 :(得分:0)

f1 = json.load(open(tweet-stream.json', 'r'))
fileWriter = csv.writer(file1 , delimiter=",",quotechar='"', quoting=csv.QUOTE_MINIMAL)
for x in f1:
    temp = [x["user"]["lang"],x["user"]["statuses_count"]]
    fileWriter.writerow(temp)
file1.close()