Here's my code:
这是我的代码:
#!/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()
and I get the following error:
我得到了以下错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 16: ordinal not in range(128)
How can I fix it?
我怎样才能修好它?
1 个解决方案
#1
4
As others suggested, you have a unicode string. Such a string returns error if you try to write it to a file. Looks like the error occurs when you save the dataFrame to a csv file.
正如其他人建议的那样,您有一个unicode字符串。如果您试图将其写入文件,那么该字符串将返回错误。当您将dataFrame保存到csv文件时,就会出现错误。
To solve this problem, you will first need to convert the string to unicode. You could write a function such as:
要解决这个问题,首先需要将字符串转换为unicode。你可以写一个函数,例如:
def change_text(text):
return text.encode('utf-8') # assuming the encoding is UTF-8
You can then apply this to the column that has unicode characters as thus:
然后,您可以将其应用到具有unicode字符的列中:
df['<column_name>'] = df.apply(change_text,axis=1)
#1
4
As others suggested, you have a unicode string. Such a string returns error if you try to write it to a file. Looks like the error occurs when you save the dataFrame to a csv file.
正如其他人建议的那样,您有一个unicode字符串。如果您试图将其写入文件,那么该字符串将返回错误。当您将dataFrame保存到csv文件时,就会出现错误。
To solve this problem, you will first need to convert the string to unicode. You could write a function such as:
要解决这个问题,首先需要将字符串转换为unicode。你可以写一个函数,例如:
def change_text(text):
return text.encode('utf-8') # assuming the encoding is UTF-8
You can then apply this to the column that has unicode characters as thus:
然后,您可以将其应用到具有unicode字符的列中:
df['<column_name>'] = df.apply(change_text,axis=1)