设置抓取工具并下载推文。无法解析JSON文件

时间:2017-07-13 17:25:18

标签: python json parsing twitter

我一直在尝试解析JSON文件,它不断给我额外的数据错误。由于我是Python的新手,我不知道如何解决这个问题。似乎文件中有多个对象。如何解析它而不会出现任何错误?

编辑:(不是我的代码,但我正在尝试处理它)

import json
import csv
import io

 '''
creates a .csv file using a Twitter .json file
the fields have to be set manually
'''

data_json = io.open('filename', mode='r', encoding='utf-8').read() #reads in 
the JSON file
data_python = json.loads(data_json)

csv_out = io.open('filename', mode='w', encoding='utf-8') #opens csv file


fields = u'created_at,text,screen_name,followers,friends,rt,fav' #field 
names
csv_out.write(fields)
csv_out.write(u'\n')

for line in data_python:

#writes a row and gets the fields from the json object
#screen_name and followers/friends are found on the second level hence two 
get methods
row = [line.get('created_at'),
       '"' + line.get('text').replace('"','""') + '"', #creates double 
quotes
       line.get('user').get('screen_name'),
       unicode(line.get('user').get('followers_count')),
       unicode(line.get('user').get('friends_count')),
       unicode(line.get('retweet_count')),
       unicode(line.get('favorite_count'))]

row_joined = u','.join(row)
csv_out.write(row_joined)
csv_out.write(u'\n')




csv_out.close()

编辑2:我发现了另一个解析它的方法,但我无法保存输出。有什么建议吗?

import json
import re

json_as_string = open('filename.json', 'r')
# Call this as a recursive function if your json is highly nested
lines = [re.sub("[\[\{\]]*", "", one_object.rstrip()) for one_object in 
json_as_string.readlines()]

json_as_list = "".join(lines).split('}')
for elem in json_as_list:
if len(elem) > 0:
    print(json.loads(json.dumps("{" + elem[::1] + "}")))

0 个答案:

没有答案