python - UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 16: ordinal not in range(128) -
python - UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 16: ordinal not in range(128) -
here's code:
#!/usr/bin/python # import modules import pandas pd import requests import numpy np # set ipython's max row display pd.set_option('display.max_row', 1000) # insert crisisnet api key api_key = xxxxxxxxxxxxxxx # insert crisisnet request api api_url = 'http://api.crisis.net/item?sources=twitter&tags=weather' # create request header headers = {'authorization': 'bearer ' + api_key} # define how many info points want total = 10000 # create dataframe request info go df = pd.dataframe() # define function called data, def get_data(offset=0, limit=100, df=none): # create variable called url, has request info, url = api_url + '&offset=' + str(offset) + '&limit=' + str(limit) # variable called r, request data, r = requests.get(url, headers=headers) # convert request info dataframe, x = pd.dataframe(r.json()) # expand dataframe x = x['data'].apply(pd.series) # add together dataframe's rows main dataframe, df, defined outside function df = df.append(x, ignore_index=true) # then, if total larger request limit plus offset, if total > offset + limit: # run function time homecoming get_data(offset + limit, limit, df) # if not, end function homecoming df # run function df = get_data(df=df) # check number of info points retrieved len(df) # check duplicate info points df['id'].duplicated().value_counts() # drop duplicate info points df = df.dropna(how='all') df.to_csv('twitterweather.csv') # view first 10 info points print df.head()
and next error:
unicodeencodeerror: 'ascii' codec can't encode character u'\xf3' in position 16: ordinal not in range(128)
how can prepare it?
as others suggested, have unicode string. such string returns error if seek write file. looks error occurs when save dataframe csv file.
to solve problem, first need convert string unicode. write function such as:
def change_text(text): homecoming text.encode('utf-8') # assuming encoding utf-8
you can apply column has unicode characters thus:
df['<column_name>'] = df.apply(change_text,axis=1)
python
Comments
Post a Comment