python - UnicodeEncodeError:'ascii'编解码器无法对位置 0-2 中的字符进行编码:序数不在范围内(128)与 python
问题描述
当我通过按单词搜索数据从 twitter 测试数据挖掘时,我遇到了一个问题。
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
这段代码出错了
retweet = "-filter:retweets"
query = "#Thailand" + retweet
df = pd.DataFrame(columns = ["create_at","user","location","text", "retweet_count", "favourite_count","hashtag","follower","source"])
for tweet in tweepy.Cursor(api.search, q = query,result_type="recent", tweet_mode='extended').items(100):
entity_hashtag = tweet.entities.get('hashtags')
hashtag = ""
for i in range(0, len(entity_hashtag)):
hashtag = hashtag + "/" + entity_hashtag[i]["text"]
re_count = tweet.retweet_count
create_at = tweet.created_at
user = tweet.user.screen_name
source = tweet.source
location = tweet.user.location
follower = tweet.user.followers_count
try:
text = tweet.retweeted_status.full_text
fav_count = tweet.retweeted_status.favorite_count
except:
text = tweet.full_text
fav_count = tweet.favorite_count
new_column = pd.Series([create_at,user,location,text, re_count, fav_count,hashtag,follower,source], index = df.columns)
df = df.append(new_column, ignore_index = True)
df.to_csv(date_time+".csv")
为什么会有这个问题?
解决方案
尝试将系统默认编码设置为utf-8
scipt 的开头,以下应将默认编码设置为 utf-8 。
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
推荐阅读
- android - 如何一次将 Firebase ValueEventListner 添加到多个节点并相应地收到任何数据更改的通知
- python - PyAv av.open() 指定使用的编解码器
- javascript - 如何使我的前缀适用于小写和大写?
- javascript - 为什么我无法获得在 Vue JS 中使用 v-for 的基本示例?
- swift - 从 Firestore 获取用户配置文件的数据(swift、iOS、Xcode、firebase/firestore)
- python - 将一系列包含混合内容的CSV文件读入Python进行数据分析
- python - Scrapy Html 表到带有重复标题的 DataFrame
- php - 致命错误:无法在 php 中对表达式的结果使用 isset()(您可以改用“null !== expression”)
- powershell - Powershell不会将击键传递给cli窗口
- javascript - 从Javascript卡到HTML,一定有更好的方法(工作)