过去几天我一直在研究如何实现这一目标,但无济于事 .
我有一个带有大量json对象的JSON文件,如下所示:
[{
"tweet": "@SHendersonFreep @realDonaldTrump watch your portfolios go to the Caribbean banks and on to Switzerland. Speculation without regulation",
"user": "DGregsonRN"
},{
"tweet": "RT @CodeAud: James Mattis Vs Iran.\n\"The appointment of Mattis by @realDonaldTrump got the Iranian military leaders' more attention\". https:\u2026",
"user": "American1765"
},{
"tweet": "@realDonaldTrump the oyou seem to be only fraud I see is you, and seem scared since you want to block the recount???hmm cheater",
"user": "tgg216"
},{
"tweet": "RT @realDonaldTrump: @Lord_Sugar Dopey Sugar--because it was open all season long--you can't play golf in the snow, you stupid ass.",
"user": "grepsalot"
},{
"tweet": "RT @Prayer4Chandler: @realDonaldTrump Hello Mr. President, would you be willing to meet Chairman #ManHeeLee of #HWPL to discuss the #PeaceT\u2026",
"user": "harrymalpoy1"
},{
"tweet": "RT @realDonaldTrump: Thank you Ohio! Together, we made history \u2013 and now, the real work begins. America will start winning again! #AmericaF\u2026",
"user": "trumpemall"
}]
我试图访问每个键和值,并将它们写入csv文件 . 我相信使用 json.loads(json.dumps(file))
应该以普通的json格式工作,但因为有一个对象数组,我似乎无法访问每个单独的对象 .
converter.py :
import json
import csv
f = open("tweets_load.json",'r')
y = json.loads(json.dumps(f.read(), separators=(',',':')))
t = csv.writer(open("test.csv", "wb+"))
# Write CSV Header, If you dont need that, remove this line
t.writerow(["tweet", "user"])
for x in y:
t.writerow([x[0],x[0]])
grab_tweets.py :
import tweepy
import json
def get_api(cfg):
auth = tweepy.OAuthHandler(cfg['consumer_key'], cfg['consumer_secret'])
auth.set_access_token(cfg['access_token'], cfg['access_token_secret'])
return tweepy.API(auth)
def main():
cfg = {
"consumer_key" : "xxx",
"consumer_secret" : "xxx",
"access_token" : "xxx",
"access_token_secret" : "xxx"
}
api = get_api(cfg)
json_ret = tweepy.Cursor(api.search, q="@realDonaldTrump",count="100").items(100)
restapi =""
for tweet in json_ret:
rest = json.dumps({'tweet' : tweet.text,'user' :str(tweet.user.screen_name)},sort_keys=True,indent=4,separators=(',',': '))
restapi = restapi+str(rest)+","
f = open("tweets.json",'a')
f.write(str(restapi))
f.close()
if __name__ == "__main__":
main()
到目前为止的输出看起来像:
tweet,user^M
{,{^M
"
","
"^M
, ^M
, ^M
, ^M
, ^M
"""",""""^M
t,t^M
w,w^M
e,e^M
e,e^M
t,t^M
"""",""""^M
:,:^M
, ^M
"""",""""^M
R,R^M
T,T^M
, ^M
@,@^M
r,r^M
e,e^M
a,a^M
l,l^M
D,D^M
o,o^M
n,n^M
a,a^M
l,l^M
我究竟做错了什么?
1 回答
事实证明这是json.dumps(),应该更多地了解它的作用!谢谢 .