Я изучал последние несколько дней о том, как достичь этого, но безрезультатно.Доступ к ключевому значению из массива объектов JSON Python
У меня есть 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
Что именно я делаю неправильно?
'json.dumps', как вы думаете, что это делает? – njzk2
Проблема '^ M' является проблемой кодирования. Кроме того, похоже, что вы повторяли строку –
Я не могу представить, какую проблему вы пытаетесь решить, выполнив 'json.loads (json.dumps (file))'. –