当前位置: 首页 > 知识库问答 >
问题:

UnicodeEncodeError:“ASCII”编解码器无法对位置0-2中的字符进行编码:序号不在范围(128)中,使用python[重复]

蒋栋
2023-03-14

当我测试从twitter上的数据挖掘时,我有一个麻烦,我通过Word搜索数据

It错误UnicodeEncodeError:“ASCII”编解码器无法对位置0-2的字符进行编码:序号不在范围(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")

为什么会有这样的问题?

共有1个答案

南门志
2023-03-14

尝试将系统默认编码设置为utf-8,在scipt的开头,下面应该将默认编码设置为utf-8。

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
 类似资料: