1

I'am trying to export as csv a pandas dataframe with the function:

outcome.to_csv("/Users/john/out_1.csv")

I get the following error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 191: ordinal not in range(128)

how do I go to position 191 to check what's wrong?

Many thanks

Blue Moon
  • 4,421
  • 20
  • 52
  • 91
  • possible duplicate of ["UnicodeEncodeError: 'ascii' codec can't encode character"](http://stackoverflow.com/questions/1652904/unicodeencodeerror-ascii-codec-cant-encode-character) – Alex Kroll Jul 30 '15 at 11:24
  • @johnred it's position 191 in a string that is in your DataFrame. Perhaps you could put the whole stack trace, the DataFrame itself? – galath Jul 30 '15 at 11:58
  • Could you try adding encoding='utf-8' to the function and see if that works? Eg outcome.to_csv("/Users/john/out_1.csv", encoding="utf-8") – measureallthethings Jul 30 '15 at 12:47
  • 1
    if you open an ipython console and type: `print u'\u2019'` you will see what kind of character it is you are looking for. You should probably just start using python3 – firelynx Jul 30 '15 at 13:48

1 Answers1

1
    outcome.to_csv("/Users/john/out_1.csv",encoding="utf-8")

On referring to the documentation of pandas.to_csv, we have the following details. It seems that for Python 2.7 the default is "ascii" which needs to be overridden to "utf-8"

encoding : string, optional

A string representing the encoding to use in the output file, defaults to ‘ascii’ on Python 2 and ‘utf-8’ on Python 3.

Anant Gupta
  • 1,090
  • 11
  • 11