Pandas.to_csv给出错误'ascii'编解码器不能编码位置8中的字符u'\ u2013':序数不在范围内(128)

时间:2017-03-28 19:05:29

标签: python pandas

我正在尝试将panda数据帧保存到csv,但它失败并出现错误:

df.to_csv(location, sep='|', index=False, header=True)

'ascii'编解码器无法对位置8中的字符u'\ u2013'进行编码:序数不在范围内(128)

我有pandas版本:

>>> import pandas as pd
>>> pd.__version__
u'0.19.2'
>>>

在另一台机器上,相同的命令有效。安装的pandas版本为0.18.1

>>> import pandas as pd
>>> pd.__version__
u'0.18.1'
>>>

我知道添加encoding ='utf-8'会让我完成错误。 但是我想知道最近是否有更改导致后续版本的熊猫失败。

谢谢,

1 个答案:

答案 0 :(得分:1)

来自https://github.com/pandas-dev/pandas/blob/v0.18.1/pandas/core/frame.py 我们发现:

formatter = fmt.CSVFormatter(self, path_or_buf,
                                     line_terminator      = line_terminator,
                                     sep                  = sep,
                                     encoding             = encoding,
                                     compression          = compression,
                                     quoting              = quoting,
                                     na_rep               = na_rep,
                                     float_format         = float_format,
                                     cols                 = columns,
                                     header               = header,
                                     index                = index,
                                     index_label          = index_label,
                                     mode                 = mode,
                                     chunksize            = chunksize,
                                     quotechar            = quotechar,
                                     engine               = kwds.get("engine"),
                                     tupleize_cols        = tupleize_cols,
                                     date_format          = date_format,
                                     doublequote          = doublequote,
                                     escapechar           = escapechar,
                                     decimal              = decimal     ) 

我们发现:https://github.com/pandas-dev/pandas/blob/v0.19.2/pandas/core/frame.py

formatter = fmt.CSVFormatter(self,  path_or_buf,
                                    line_terminator =line_terminator,
                                    sep             =sep,
                                    encoding        =encoding,
                                    compression     =compression,
                                    quoting         =quoting,
                                    na_rep          =na_rep,
                                    float_format    =float_format,
                                    cols            =columns,
                                    header          =header,
                                    index           =index,
                                    index_label     =index_label,
                                    mode            =mode,
                                    chunksize       =chunksize,
                                    quotechar       =quotechar,

                                    tupleize_cols   =tupleize_cols,
                                    date_format     =date_format,
                                    doublequote     =doublequote,
                                    escapechar      =escapechar,
                                    decimal         =decimal)                                     

唯一的区别是“引擎”参数...现在,我们应该更深入地研究这个“引擎”参数:-( 在这里的某个地方:https://github.com/pandas-dev/pandas/blob/v0.18.1/pandas/formats/format.py

在这里: https://github.com/pandas-dev/pandas/blob/v0.19.2/pandas/formats/format.py

祝你好运!