UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128) with python
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128) with 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
,以下应将默认编码设置为 utf-8。
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
您没有提及您使用的是哪个版本的 Python,但我会在此处查看 Python 关于该主题的文档:https://www.python.org/dev/peps/pep-0263/(对于 Python 2)
从那里开始:
要定义源代码编码,必须在源文件中作为文件的第一行或第二行放置魔术注释,例如:
# coding=<encoding name>
或(使用流行编辑器识别的格式):
#!/usr/bin/python
# -*- coding: <encoding name> -*-
或:
#!/usr/bin/python
# vim: set fileencoding=<encoding name> :
我在某些情况下使用过这个版本:
#!/usr/bin/python
# -*- coding: <encoding name> -*-
也就是说,某些函数,尤其是 str()
不应与 unicode 一起使用。更喜欢 unicode()
代替。使用第三方库时,您将不得不检查他们的文档,如果他们的文档有限,可能还要查看他们的来源。
当我通过逐字搜索数据来测试来自 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
,以下应将默认编码设置为 utf-8。
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
您没有提及您使用的是哪个版本的 Python,但我会在此处查看 Python 关于该主题的文档:https://www.python.org/dev/peps/pep-0263/(对于 Python 2)
从那里开始:
要定义源代码编码,必须在源文件中作为文件的第一行或第二行放置魔术注释,例如:
# coding=<encoding name>
或(使用流行编辑器识别的格式):
#!/usr/bin/python
# -*- coding: <encoding name> -*-
或:
#!/usr/bin/python
# vim: set fileencoding=<encoding name> :
我在某些情况下使用过这个版本:
#!/usr/bin/python
# -*- coding: <encoding name> -*-
也就是说,某些函数,尤其是 str()
不应与 unicode 一起使用。更喜欢 unicode()
代替。使用第三方库时,您将不得不检查他们的文档,如果他们的文档有限,可能还要查看他们的来源。