无法在Python上获取我想要的数据

时间:2017-11-10 13:35:01

标签: python json twitter

我是Spyder的Python用户。我想从记事本转换数据(推文),并将转换后的数据输出到其他记事本。它的代码就是这样。它将生成简单的数据,例如{created at: date, user_name, unicode..} -> user_name, data

try:
    import json
except ImportError:
    import simplejson as json

tweets_filename = 'C:/Users/siri_0.txt' #unconverted data
tweets_file = open(tweets_filename, "r")

for line in tweets_file:
    try:
        tweet = json.loads(line.strip())
        if 'text' in tweet: 
            print (tweet['id']) 
            print (tweet['created_at']) 
            print (tweet['text']) 
            print (tweet['user']['id']) 
            print (tweet['user']['name']) 
            print (tweet['user']['screen_name']) 
            hashtags = []
            for hashtag in tweet['entities']['hashtags']:
                hashtags.append(hashtag['text'])
            print(hashtags)

            output = "C:/Users/fn_siri.txt"
            #I want to put the converted data here.
            out_file = open(output, 'a')
            out_file.write(line)
            out_file.close()

    except:
        continue

不幸的是,C:/Users/fn_siri.txt只能包含'未转换的数据'。如何更改包含转换数据的代码?

2 个答案:

答案 0 :(得分:1)

try:
    import json
except ImportError:
    import simplejson as json

tweets_filename = 'C:/Users/siri_0.txt' #unconverted data
tweets_file = open(tweets_filename, "r")
for line in tweets_file:
    try:
        tweet = json.loads(line.strip())
        out_file = open(output, 'a')
        if 'text' in tweet: 
            print (tweet['id'],) 
            print (tweet['created_at']) 
            print (tweet['text']) 
            print (tweet['user']['id']) 
            print (tweet['user']['name']) 
            print (tweet['user']['screen_name']) 
            hashtags = []
            for hashtag in tweet['entities']['hashtags']:
                hashtags.append(hashtag['text'])
            output = "C:/Users/fn_siri.txt"
            print(hashtags,file=out_file)
            #I am assuming the converted data you want to write to out_file is hashtags
            #out_file.write(line)# why are you writing old data here ...
            out_file.close()
    except:
        continue

答案 1 :(得分:1)

您正在向输出文件写出CMD,这是您未转换的输入,而不是只写入您想要的数据。

所以,如果你想写出用户名,后跟一个逗号,然后是例如在文本中,您需要将line替换为:

out_file.write(line)

您需要最后的out_file.write(tweet['user']['name'] + "," + tweet['text'] + "\n")以确保在每行数据后都有一个新行

相关问题