gpt4 book ai didi

python - 扫描字符串文字错误时 EOL

转载 作者:太空宇宙 更新时间:2023-11-04 02:50:27 25 4
gpt4 key购买 nike

所以我正在尝试使用这段代码

我收到错误 SyntaxError: EOL while scanning string literal

代码:

def clean_tweet(self, tweet):
'''
Utility function to clean tweet text by removing links, special characters
using simple regex statements.
'''
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])
|(\w+:\/\/\S+)", " ", tweet).split())

我错过了什么?

其次,当我运行下面这部分代码时出现错误

TypeError: 不支持的操作数类型 -: 'list' 和 'list'

代码:

 # picking positive tweets from tweets
ptweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive']
# percentage of positive tweets
print("Positive tweets percentage: {} %".format(100*len(ptweets)/len(tweets)))
# picking negative tweets from tweets
ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative']
# percentage of negative tweets
print("Negative tweets percentage: {} %".format(100*len(ntweets)/len(tweets)))
# percentage of neutral tweets
print("Neutral tweets percentage: {} % \
".format(100*len(tweets - ntweets - ptweets)/len(tweets)))
# percentage of neutral tweets
print("Neutral tweets percentage: {} % \
".format(100*len(tweets - ntweets - ptweets)/len(tweets)))

是不是因为我试图从列表中减去列表,如果是这样,numpy 有帮助吗?

最佳答案

专注于:

print("Neutral tweets percentage: {} % \
".format(100*len(tweets - ntweets - ptweets)/len(tweets)))

显然 tweetsntweetsptweets 都是列表,因此具有长度。然后我认为你可以将其重写为:

print("Neutral tweets percentage: {} % \
".format(100*(len(tweets) - len(ntweets) - len(ptweets))/len(tweets)))

关于python - 扫描字符串文字错误时 EOL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44053032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com