如何通过Python程序运行Excel文档



我正试图通过情绪分析程序(通过textblob(运行几百条Trip Advisor评论,这样它就会阅读每条评论并为其提供情绪。该程序已经运行了,你可以键入一个句子,它会返回情绪(即积极、消极、中性(。我想通过程序运行excel文档,而不必手动键入每次审核。理想情况下,该程序在每次评论中都会返回一种情绪。。。。。我该怎么做?

这是我已经拥有的代码。。。

from textblob import TextBlob
import string
z = 10
poscounter = 0
negcounter = 0
neucounter = 0
totalsentences = 0
while z > 0:

y = input("Type your sentence ").lower()  
y = y.translate(str.maketrans('', '', string.punctuation))
y1 = TextBlob(y)
sentCheck = y1.sentiment.polarity
if y == "stop":

print(f"Positive Sentiment: {(poscounter/totalsentences) * 100} %")
print(f"Negative Sentiment: {(negcounter/totalsentences) * 100} %")
print(f"Neutral Sentiment: {(neucounter/totalsentences) * 100} %")
exit()


xplitIt = y.split(" ")

for something in xplitIt:
if something == "crowded" or something == "busy" or something == "crowds" or something == "hate" or something == "hated":
print("negative")
sentCheck = -0.1
break


if sentCheck==0:
print("neutral")
neucounter+=1
elif sentCheck>0 and sentCheck <=1:
print("positive")
poscounter+=1
elif sentCheck == -0.1 or sentCheck < 0:
negcounter+=1
if sentCheck != -0.1:
print("negative")

totalsentences = totalsentences + 1

有一些库可以从word文档中读取文本。您可以查看此答案中建议的textract

你阅读的文本可能会有多条评论,你必须将其拆分成一个评论列表(例如,使用python的split()函数(。然后,您可以将这些评论中的每一个作为y输入到您的算法中。

如何处理输入取决于文档的结构。如果你需要进一步的帮助,你应该在你的问题中包括一个例子。

最新更新