2014-10-08 89 views
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) 

我該如何解決?

+0

某處,你有你要麼一個'unicode'對象儘管它已經被解碼,或者試圖使用,就好像它是一個'str'(例如,將它添加到一個'str'中,或者將它傳遞給一個需要'str'的函數')來嘗試「解碼」,其中你需要對它進行編碼。向我們展示整個回溯過程,它會告訴你哪個表達式導致了這種情況,而不是讓我們猜測。 – abarnert 2014-10-08 23:07:58

+0

你能發佈完整的追溯和鏈接到你的數據嗎?它看起來像你可能想要「解碼」你的文本,也就是說,你可能想要將它轉換爲unicode。不過,爲此,您需要知道正在處理的數據的字符編碼。你知道字符編碼(例如「utf-8」)嗎? – duhaime 2014-10-08 23:16:52

+1

@Mona Jalal,我從你的問題中刪除了你的api密鑰 – 2014-10-08 23:29:59

回答

4

正如其他人所建議的,您有一個unicode字符串。如果您嘗試將其寫入文件,則這樣的字符串會返回錯誤。看起來像將數據幀保存到csv文件時發生錯誤。

要解決此問題,您首先需要將字符串轉換爲unicode。你可以寫一個函數,例如:

def change_text(text): 
    return text.encode('utf-8') # assuming the encoding is UTF-8 

然後,您可以將此到具有Unicode字符作爲這樣的列:

df['<column_name>'] = df.apply(change_text,axis=1) 
相關問題