2016-12-05 17 views
0

私はこれを達成するために過去数日間、無駄なことを研究してきました。オブジェクトのJSON配列からのキー値へのアクセス

私はそうのような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:まさに私がやっている何

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 

 

    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() 

出力これまでは以下のように探しています違う?

+1

'json.dumps'これはどういうのですか? – njzk2

+0

'^ M 'はエンコーディングの問題です。それ以外の場合は、文字列を反復したように見えます。 –

+1

'json.loads(json.dumps(file)) 'を実行することで、あなたが解決しようとしている問題を想像することはできません。 –

答えて

0

それはjson.dumps()だったことが判明しました。ありがとう。

関連する問題