我正在使用我在网上找到的一段代码,它使用Python中的TextBlob来分析Tweets的情绪,它生成的JSON文件使用单引号,而我需要它使用双引号。我不知道如何在代码中改变这一点,所以我想知道是否有比我知识更丰富的人能够提供帮助。你知道吗
我已经尝试过用双引号替换Notepad++中的单引号,但显然这有点棘手,因为我不想替换tweet中的实际引号和撇号。你知道吗
"""
Author: Stephen W. Thomas
Perform sentiment analysis using TextBlob to do the heavy lifting.
"""
from textblob import TextBlob
import csv
import re
import operator
tweets = []
def strip_non_ascii(string):
stripped = (c for c in string if 0 < ord(c) < 127)
return ''.join(stripped)
#LOAD AND CLEAN DATA
with open("bachelormonday_tweets.csv", "rt") as csvfile:
reader = csv.reader(csvfile, delimiter=",")
next(reader)
for row in reader:
tweet= dict()
tweet["orig"]=row[0]
tweet["TextBlob"] = TextBlob(tweet["clean"])
tweets.append(tweet)
# DEVELOP MODELS
for tweet in tweets:
tweet["polarity"] = float(tweet["TextBlob"].sentiment.polarity)
tweet["subjectivity"] = float(tweet["TextBlob"].sentiment.subjectivity)
if tweet["polarity"] >= 0.1:
tweet["sentiment"] = 'positive'
elif tweet["polarity"] <= -0.1:
tweet["sentiment"] = 'negative'
else:
tweet["sentiment"] = 'neutral'
tweets_sorted = sorted(tweets, key=lambda k: k["polarity"])
print(tweets)
我想要的是一个文本输出,在元素周围有双引号,但我得到的结果是这样的:
{
'orig': 'Who else is waiting for that fence jump from #TheBachelor?? Show us the goods already! @chrisbharrison @coltonpic.twitter.com/x2sMwgmVxg',
'clean': 'who else is waiting for that fence jump from #thebachelor?? show us the goods already! @chrisbharrison @coltonpic.twitter.com/x2smwgmvxg',
'TextBlob': TextBlob("who else is waiting for that fence jump from #thebachelor?? show us the goods already! @chrisbharrison @coltonpic.twitter.com/x2smwgmvxg"),
'polarity': 0.0,
'subjectivity': 0.0,
'sentiment': 'neutral'
},
使用
json
模块。您可能必须省略TextBlob
元素,因为它没有JSON表示。你知道吗相关问题 更多 >
编程相关推荐