错误处理-在 pandas 中 [英] Error handling ñ in pandas

查看:61
本文介绍了错误处理-在 pandas 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个读取csv文件并使用pandas库创建数据透视表的脚本.

I am writing a script that reads a csv file and uses the pandas library to create a pivot table.

我一直收到错误消息("utf-8"编解码器无法解码位置6:无效的连续字节中的字节0xf1),该错误已链接回csv文件中的名称之一中的ñ"字样

I keep receiving an error ('utf-8' codec can't decode byte 0xf1 in position 6: invalid continuation byte) that I have linked back to the use of 'ñ' in one of the names in the csv file.

我已经搜索了数小时,试图找到一种解决方法.我尝试将编码类型包含在我的pandas.read_csv中,但没有运气.

I have searched for hours trying to find a way to handle this. I have tried including the encoding type in my pandas.read_csv and have had no luck.

这是我的代码:

df = pandas.read_csv(
            os.path.join(wd,'Birthday_%s.csv' % datesuffix),
            encoding='utf-8')
pivot = pandas.pivot_table(df,
            index=['ClientID','ClientName','Branch'],
            values=['EmailAddress'],
            aggfunc='count',
            margins=True)
pivotlocation = os.path.join(wd,'BirthdayPivot.csv')
pivot.to_csv(pivotlocation)

任何帮助将不胜感激.

这是引起问题的相关行.

Here is the line in question that is causing the issue.

ClientID | ClientName    | Branch        | Name     | EmailAddress
5555     | ExampleClient | ExampleBranch | Avendaño | email@email.com

似乎是引起问题的原因是名称列(包含Avendaño").

It is the name column (containing 'Avendaño') that seems to be causing the issues.

推荐答案

正确的编码可能是"latin-1",因此您可能需要考虑:

The proper encoding may be 'latin-1' so you may want to consider:

df = pandas.read_csv(
            os.path.join(wd,'Birthday_%s.csv' % datesuffix),
            encoding='latin-1')

这篇关于错误处理-在 pandas 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆