首页 > 解决方案 > Google Colab 上熊猫的 ANSI 编码?

问题描述

所以有一个名为“students_data.txt”的文件,它以制表符分隔的形式保存记录,并且文件本身使用 ANSI 编码进行编码。在我的本地 Windows 机器上(Windows 无条件支持 ANSI :))我可以使用 pandas 轻松读取文件,如下所示:

pd.read_csv(input_directory+'students_data.txt', '\t', encoding='ANSI')

数据被读取,一切都很好,但是在 google colab 上它会产生这个错误:

LookupError: unknown encoding: ansi on pandas

有趣的是,我的机器和 colab 的 pandas 版本是相同的。所以我的想法是由于 Colab 机器的性质,我无法解码 ANSI 文件......

所以我的问题是:

标签: pythonpandasencodinggoogle-colaboratory

解决方案


尝试使用 ISO-8859-1 编码

pd.read_csv(input_directory+'students_data.txt', '\t', encoding='ISO-8859-1')

原来这是解决方案,因为 ANSI 是微软专有的,只能由 Microsoft Windows 系统上的 pandas 识别。另一方面,Google colab 运行 linux(可以通过 os,system() 进行检查)。ANSI 是 ISO-8859-1 的超集,因此它很有可能适用于 ANSI 文件。详情:这里


推荐阅读