1
这里是我的代码:UnicodeEncodeError: 'ASCII' 编解码器不能编码字符U ' XF3' 在第16位:在范围序数不(128)
#!/usr/bin/python
# Import modules
import pandas as pd
import requests
import numpy as np
# Set ipython's max row display
pd.set_option('display.max_row', 1000)
# Insert your CrisisNET API key
api_key = xxxxxxxxxxxxxxx
# Insert your CrisisNET request API
api_url = 'http://api.crisis.net/item?sources=twitter&tags=weather'
# Create the request header
headers = {'Authorization': 'Bearer ' + api_key}
# Define how many data points you want
total = 10000
# Create a dataframe where the request data will go
df = pd.DataFrame()
# Define a function called get data,
def get_data(offset=0, limit=100, df=None):
# create a variable called url, which has the request info,
url = api_url + '&offset=' + str(offset) + '&limit=' + str(limit)
# a variable called r, with the request data,
r = requests.get(url, headers=headers)
# convert the request data into a dataframe,
x = pd.DataFrame(r.json())
# expand the dataframe
x = x['data'].apply(pd.Series)
# add the dataframe's rows to the main dataframe, df, we defined outside the function
df = df.append(x, ignore_index=True)
# then, if the total is larger than the request limit plus offset,
if total > offset + limit:
# run the function another time
return get_data(offset + limit, limit, df)
# but if not, end the function
return df
# Run the function
df = get_data(df=df)
# Check the number of data points retrieved
len(df)
# Check for duplicate data points
df['id'].duplicated().value_counts()
# Drop all duplicate data points
df = df.dropna(how='all')
df.to_csv('TwitterWeather.csv')
# View the first 10 data points
print df.head()
,我得到以下错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 16: ordinal not in range(128)
我该如何解决?
某处,你有你要么一个'unicode'对象尽管它已经被解码,或者试图使用,就好像它是一个'str'(例如,将它添加到一个'str'中,或者将它传递给一个需要'str'的函数')来尝试“解码”,其中你需要对它进行编码。向我们展示整个回溯过程,它会告诉你哪个表达式导致了这种情况,而不是让我们猜测。 – abarnert 2014-10-08 23:07:58
你能发布完整的追溯和链接到你的数据吗?它看起来像你可能想要“解码”你的文本,也就是说,你可能想要将它转换为unicode。不过,为此,您需要知道正在处理的数据的字符编码。你知道字符编码(例如“utf-8”)吗? – duhaime 2014-10-08 23:16:52
@Mona Jalal,我从你的问题中删除了你的api密钥 – 2014-10-08 23:29:59