有没有办法在 Python/Jupyter 中创建类似大小写的 IF 语句,以使用 Tweepy 监视推文中的子字符串?
Is there a way to create a case-like IF statement in Python/Jupyter to monitor substrings in Tweets using Tweepy?
我试图通过将推文导出到 .csv 来监控多个企业 Twitter 帐户,以查看包含企业名称的推文的积极性或消极性,然后将其可视化。
为了让我自己更轻松,我只为每条推文分配一个数字,介于 1(负面)- 10(正面)之间,但是我编写的代码没有提供任何反馈(保持为 0) ,陷入 For 循环,或出现语法错误。
使用 Jupyter notebook 我尝试创建一个 10 行的 If/Elif 语句 - 由于 Python 没有 case 语句,并且在 'get Tweets' 方法中插入了这段代码以及 'write csv' 方法。
获取推文
api = tweepy.API(auth)
query = "ASOS"
language = "en"
results = api.search(q=query, lang=language, count=100)
for tweet in results:
if (not tweet.retweeted) and ('RT @' not in tweet.text):
print(tweet.user.screen_name,"Tweeted:",tweet.text,**rating**)
print()
写入 CSV
import csv
api = tweepy.API(auth)
csvFile = open('ASOS with emojis1.csv', 'a')
csvWriter = csv.writer(csvFile)
results = api.search(q=query, lang=language, count=100)
for tweet in results:
if (not tweet.retweeted) and ('RT @' not in tweet.text):
csvWriter.writerow([tweet.created_at, tweet.user.screen_name, tweet.text, **rating**])
csvFile.close()
If/Elif 我写的声明
rating = '0'
if 'abysmal' or 'appalling' or 'dreadful' or 'awful' or 'terrible' or 'very bad' or 'really bad' or '' or '' or '' in tweet.text:
(rating = '1')
elif 'rubbish' or 'unsatisfactory' or 'bad' or 'poor' or '' or '' or ':(' or '):' or '' in tweet.text:
(rating = '2')
elif 'quite bad' or 'pretty bad' or 'somewhat bad' or 'below average' or '' or '' or '☹️' or '' or '' in tweet.text:
(rating = '3')
elif 'mediocre' or '' or '' or '' or '' or '' in tweet.text:
(rating = '4')
elif 'average' or 'not bad' or 'fair' or 'alright' or 'ok' or 'satisfactory' or 'fine' or 'somewhat good' or '' or '' or '' or '' or '' or '' or '' or 'omg' in tweet.text:
(rating = '5')
elif 'quite good' or 'decent' or 'above average' or 'pretty good' or 'good' or '' or '' or '' or '' or '' in tweet.text:
(rating = '6')
elif 'great' or 'gr8' or 'really good' or 'rlly good' or 'very good' or 'v good' or '' or '☺️' or '' or '' or '' or '' or '' ':)' or '(:' or '' or '' or '' or '' or '' in tweet.text:
(rating = '7')
elif 'awesome' or 'fantastic' or '' or '' or '' or '' or '❤' or '♥' or '' or '' or '✅' or '' or '' or '' or '✨' in tweet.text:
(rating = '8')
elif 'superb' or 'brilliant' or 'incredible' or 'excellent' or 'oustanding' or '' or '' or '' or '' in tweet.text:
(rating = '9')
elif 'perfect' in tweet.text:
(rating = '10')
else:
(rating = 'N/A')
预期:生成包含
中各种数字的 .csv 文件
实际:(评分 = '1')语法错误:语法无效
你的条件没有正常工作。要链接条件:
mylist = [1, 2, 3]
# Note that the full condition must be specified for
# each desired conditional
if 1 in mylist or 2 in mylist or 3 in mylist:
print("True")
# True
您所使用的问题在于您正在按照您说的方式处理逻辑,而不是解释器如何阅读它。例如:
if 'a' or 'b':
print('True')
# True
填充的字符串充当 True
并将评估您的条件,因此应进行修改以指定整个条件:
# Evaluates to True, though it's not what you want
if 'a' and 'b' in 'bc':
print(True) # This is not what you want, but 'a' is read as 'true'
# True
if 'a' in 'bc' and 'b' in 'bc':
print(True)
# Doesn't print True because 'a' in 'bc' is False
any
函数在这里可以提供帮助,因为它会查看是否有任何值的计算结果为 True
:
mylist = [1, 2, 3]
if any([i in mylist for i in range(2,5)]):
print("True")
# True
此外,变量赋值不需要括号:
if 'abysmal' in tweet.text or 'horrible' in tweet.text:
rating = 0
elif ...:
rating = 1
# So on and so forth
我试图通过将推文导出到 .csv 来监控多个企业 Twitter 帐户,以查看包含企业名称的推文的积极性或消极性,然后将其可视化。
为了让我自己更轻松,我只为每条推文分配一个数字,介于 1(负面)- 10(正面)之间,但是我编写的代码没有提供任何反馈(保持为 0) ,陷入 For 循环,或出现语法错误。
使用 Jupyter notebook 我尝试创建一个 10 行的 If/Elif 语句 - 由于 Python 没有 case 语句,并且在 'get Tweets' 方法中插入了这段代码以及 'write csv' 方法。
获取推文
api = tweepy.API(auth)
query = "ASOS"
language = "en"
results = api.search(q=query, lang=language, count=100)
for tweet in results:
if (not tweet.retweeted) and ('RT @' not in tweet.text):
print(tweet.user.screen_name,"Tweeted:",tweet.text,**rating**)
print()
写入 CSV
import csv
api = tweepy.API(auth)
csvFile = open('ASOS with emojis1.csv', 'a')
csvWriter = csv.writer(csvFile)
results = api.search(q=query, lang=language, count=100)
for tweet in results:
if (not tweet.retweeted) and ('RT @' not in tweet.text):
csvWriter.writerow([tweet.created_at, tweet.user.screen_name, tweet.text, **rating**])
csvFile.close()
If/Elif 我写的声明
rating = '0'
if 'abysmal' or 'appalling' or 'dreadful' or 'awful' or 'terrible' or 'very bad' or 'really bad' or '' or '' or '' in tweet.text:
(rating = '1')
elif 'rubbish' or 'unsatisfactory' or 'bad' or 'poor' or '' or '' or ':(' or '):' or '' in tweet.text:
(rating = '2')
elif 'quite bad' or 'pretty bad' or 'somewhat bad' or 'below average' or '' or '' or '☹️' or '' or '' in tweet.text:
(rating = '3')
elif 'mediocre' or '' or '' or '' or '' or '' in tweet.text:
(rating = '4')
elif 'average' or 'not bad' or 'fair' or 'alright' or 'ok' or 'satisfactory' or 'fine' or 'somewhat good' or '' or '' or '' or '' or '' or '' or '' or 'omg' in tweet.text:
(rating = '5')
elif 'quite good' or 'decent' or 'above average' or 'pretty good' or 'good' or '' or '' or '' or '' or '' in tweet.text:
(rating = '6')
elif 'great' or 'gr8' or 'really good' or 'rlly good' or 'very good' or 'v good' or '' or '☺️' or '' or '' or '' or '' or '' ':)' or '(:' or '' or '' or '' or '' or '' in tweet.text:
(rating = '7')
elif 'awesome' or 'fantastic' or '' or '' or '' or '' or '❤' or '♥' or '' or '' or '✅' or '' or '' or '' or '✨' in tweet.text:
(rating = '8')
elif 'superb' or 'brilliant' or 'incredible' or 'excellent' or 'oustanding' or '' or '' or '' or '' in tweet.text:
(rating = '9')
elif 'perfect' in tweet.text:
(rating = '10')
else:
(rating = 'N/A')
预期:生成包含
中各种数字的 .csv 文件实际:(评分 = '1')语法错误:语法无效
你的条件没有正常工作。要链接条件:
mylist = [1, 2, 3]
# Note that the full condition must be specified for
# each desired conditional
if 1 in mylist or 2 in mylist or 3 in mylist:
print("True")
# True
您所使用的问题在于您正在按照您说的方式处理逻辑,而不是解释器如何阅读它。例如:
if 'a' or 'b':
print('True')
# True
填充的字符串充当 True
并将评估您的条件,因此应进行修改以指定整个条件:
# Evaluates to True, though it's not what you want
if 'a' and 'b' in 'bc':
print(True) # This is not what you want, but 'a' is read as 'true'
# True
if 'a' in 'bc' and 'b' in 'bc':
print(True)
# Doesn't print True because 'a' in 'bc' is False
any
函数在这里可以提供帮助,因为它会查看是否有任何值的计算结果为 True
:
mylist = [1, 2, 3]
if any([i in mylist for i in range(2,5)]):
print("True")
# True
此外,变量赋值不需要括号:
if 'abysmal' in tweet.text or 'horrible' in tweet.text:
rating = 0
elif ...:
rating = 1
# So on and so forth