Pound sign problem
Peter Otten
__peter__ at web.de
Mon Apr 10 09:51:21 EDT 2017
David Shi via Python-list wrote:
> In the data set, pound sign escape appears:
> u'price_currency': u'\xa3', u'price_formatted': u'\xa3525,000',
> When using table.to_csv after importing pandas as pd, an error message
> persists as follows: UnicodeEncodeError: 'ascii' codec can't encode
> character u'\xa3' in position 0: ordinal not in range(128)
The default encoding in Python 2 is ascii, and the pound sign is not part of
that.
> Can anyone help?
Specify an alternative encoding, preferably UTF-8:
>>> import pandas
>>> df = pandas.DataFrame([[u"\xa3123"], [u"\xa3321"]], columns=["Price"])
>>> df
Price
0 £123
1 £321
[2 rows x 1 columns]
>>> df.to_csv("tmp.csv", encoding="utf-8")
>>>
$ cat tmp.csv
,Price
0,£123
1,£321
$
More information about the Python-list
mailing list