我想在Python上使用out_文件

我想在Python上使用out_文件,python,json,twitter,tweepy,Python,Json,Twitter,Tweepy,我在学习Python两个月。我的目标是做情绪分析!但是自学太难了,所以我想寻求帮助 我从twitterapi收集数据,然后把数据放到记事本上。那样太长了{created_at:“Fri Nov 03 03:28:33+0000 2017”,~~id,tweet,unicode} 我在IPython控制台(Spyder)上将数据转换为简单数据。这就像“Fri Nov 03:46:46+0000 2017@user blah blah blah[hash tags]time stamp”。然后我想再

我在学习Python两个月。我的目标是做情绪分析!但是自学太难了,所以我想寻求帮助

  • 我从twitterapi收集数据,然后把数据放到记事本上。那样太长了<代码>{created_at:“Fri Nov 03 03:28:33+0000 2017”,~~id,tweet,unicode}

  • 我在IPython控制台(Spyder)上将数据转换为简单数据。这就像
    “Fri Nov 03:46:46+0000 2017@user blah blah blah[hash tags]time stamp”
    。然后我想再次将简单数据放入记事本。代码编写如下。如何更改
    out\u文件部分的代码

    try:
        import json
    
    except ImportError:
        import simplejson as json
    
    tweets_filename = 'C:/Users/ID500/Desktop/SA/Corpus/siri/siri_0.txt' #Not converted 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)
                out_file = open("C:/Users/ID500/Desktop/SA/Corpus/final/fn_siri_1.txt", 'a') # I want to put data to that path.
                out_file.write() # What can I write here?
                out_file.close()
    
        except:
            continue
    

  • 谢谢!

    您可以同时打开两个文件。不要在循环中打开一个

    比如说

    open(tweets_filename) as tweets_file, open(output, "a") as out_file:
        for line in tweets_file:
            # parse the line here 
            out_file.write(line)  
    

    您可以同时打开两个文件。不要在循环中打开一个

    比如说

    open(tweets_filename) as tweets_file, open(output, "a") as out_file:
        for line in tweets_file:
            # parse the line here 
            out_file.write(line)  
    

    您没有函数,因此return无法在那里工作。我有一个错误。我编辑了它!
    json
    是现代Python中的
    simplejson
    。在旧版本中,您可以尝试先使用(的标准库版本)导入
    simplejson
    )
    json
    作为备用。您没有函数,因此return在那里不起作用。我犯了一个错误。
    json
    是现代Python中的
    simplejson
    。在旧版本中,您可以先尝试使用(的标准库版本)导入
    simplejson
    )
    json
    作为备用。我理解你所说的。但转换后的数据不能包含在记事本中。只有保存在记事本中的未转换数据。我如何尝试此操作?显然,你需要修改行列表以执行所需操作,或者创建一个新的行列表。好的。谢谢:)我理解你所说的内容。但转换后的数据不能被删除包含在记事本中。仅保存在记事本中的未转换数据。我如何尝试此操作?您显然需要修改行列表以执行所需操作,或者创建一个新的行列表。好的。谢谢:)