python - 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 文件......
所以我的问题是:
- 如何在 Google Colab 上对 pandas 使用 ANSI 编码?
- 为什么熊猫编码取决于它使用的平台?
解决方案
尝试使用 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 文件。详情:这里
推荐阅读
- powershell - 如何过滤掉内置和不需要的组(Where-Object PS)
- python - 如何将文件夹中的所有文件作为输入(一次一个)并在处理后将它们保存到另一个文件夹中?
- go - 使用管道的 Redis 客户端计数
- javascript - 如何在 .test.js 中模拟 firebase .auth().currentUser
- c++ - 将 std::forward 用于未进一步传递给函数的通用引用是否有意义?
- javascript - cloneNode without losing event listener
- css - 对称形式
- css - 反应 ChakraUI ForwardRef 孩子忽略变体道具
- javascript - if else 条件在地图功能中无法正常工作
- reactjs - 在谷歌优化中编辑反应组件