Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
输出控制台中的不同格式与Python中保存的文件_Python_Python 3.x - Fatal编程技术网

输出控制台中的不同格式与Python中保存的文件

输出控制台中的不同格式与Python中保存的文件,python,python-3.x,Python,Python 3.x,我试图找到一份声明的情绪分析。我在网上找到了代码并对其进行了测试,结果成功了,但我很难将输出保存到文本文件中 # import SentimentIntensityAnalyzer class # from vaderSentiment.vaderSentiment module. import requests from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer def sentiment_scores(

我试图找到一份声明的情绪分析。我在网上找到了代码并对其进行了测试,结果成功了,但我很难将输出保存到文本文件中

# import SentimentIntensityAnalyzer class
# from vaderSentiment.vaderSentiment module.

import requests
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

def sentiment_scores(sentence):

    sid_obj = SentimentIntensityAnalyzer()
    sentiment_dict = sid_obj.polarity_scores(sentence)

    with open("file30_12_19.txt", "a+") as text_file:
        text_file.write(str(sentiment_dict))
        neg = "sentence was rated as ", sentiment_dict['neg'] * 100, "% Negative"
        neu = "sentence was rated as ", sentiment_dict['neu'] * 100, "% Neutral"
        pos = "sentence was rated as ", sentiment_dict['pos'] * 100, "% Positive"
        allover = "Sentence Overall Rated As "
        text_file.write(str(neg)+str(neu)+str(pos)+str(allover))
        print(str(neg)+str(neu)+str(pos)+str(allover))

        if sentiment_dict['compound'] >= 0.05:
            text_file.write("Positive")
            print("Positive")

        elif sentiment_dict['compound'] <= - 0.05:
            text_file.write("Negative")
            print("Negative")

        else:
            text_file.write("Neutral")
            print("Neutral")

        text_file.write("\n")
        text_file.close()

if __name__ == "__main__":

    sentence1 = [
        "Started off as the greatest series of all time, but had the worst ending of all time.",
        "Exquisite. 'Big Little Lies' takes us to an incredible journey with its emotional and intriguing storyline.",
        "I love Brooklyn 99 so much. It has the best crew ever!!",
        "The Big Bang Theory and to me it's one of the best written sitcoms currently on network TV.",
        "'Friends' is simply the best series ever aired. The acting is amazing.",
        "SUITS is smart, sassy, clever, sophisticated, timely and immensely entertaining!",
        "Cumberbatch is a fantastic choice for Sherlock Holmes-he is physically right (he fits the traditional reading of the character) and he is a damn good actor",
        "What sounds like a typical agent hunting serial killer, surprises with great characters, surprising turning points and amazing cast."
        "This is one of the most magical things I have ever had the fortune of viewing.",
        "I don't recommend watching this at all!"
    ]

    with open("file30_12_19.txt", "a+") as text:
        for data in sentence1:
            text.write(data)
            print(data)
            sentiment_scores(data)
            print("\n")
            text.write("\n")
#导入IntensityAnalyzer类
#来自vadertouction.vadertouction模块。
导入请求
从vadertouction.vadertouction导入感伤强度分析器
def情绪测试分数(句子):
sid_obj=情绪强度分析器()
情绪(句子)得分(句子)
打开(“file30_12_19.txt”,“a+”)作为文本文件:
文本文件写入(str(情绪文件))
neg=“句子被评定为”情绪['neg']*100,“%负面”
neu=“句子被评定为”情绪['neu']*100,“%中性”
pos=“句子被评定为”,情绪[pos']*100,“%积极”
allover=“句子整体评级为”
写入(str(neg)+str(neu)+str(pos)+str(满))
打印(负数+负数+负数+负数+负数+负数)
如果“复合物”>=0.05:
文本文件。写入(“正”)
打印(“正片”)

elif touction_dict['composite']打开同一个文件两次,同时不刷新缓冲区,因此内部写入(在
touction_scores
中)首先保存。对句子1中的数据执行两个较小的
,在
中使用
语句:
以获得更正确的代码(将上下文管理器的范围保持在最小范围内!仅在需要时!),或者在执行
情绪评分之前添加
text.flush()

    for data in sentence1:
        with open("file30_12_19.txt", "a+") as text:
            text.write(data)
            print(data)
        sentiment_scores(data)
        with open("file30_12_19.txt", "a+") as text:
            print("\n")
            text.write("\n")
或:


是缓冲区缓存导致了问题…刷新工作了…但您可能想了解字符串格式。
    with open("file30_12_19.txt", "a+") as text:
        for data in sentence1:
            text.write(data)
            text.flush()
            print(data)
            sentiment_scores(data)
            print("\n")
            text.write("\n")
            text.flush()