【问题标题】:how to transform csv files of various encoding into utf-8如何将各种编码的csv文件转换成utf-8
【发布时间】:2017-11-26 07:18:19
【问题描述】:

我从网上下载了 95 个 CSV 小文件。他们的模式应该非常相似。我正在尝试将它们与 Python pandas 连接,但是在调用 pd.read_csv 时,这些文件的各种编码会导致问题,我不确定将它们转换为一致编码的最佳方法是什么,例如UTF-8。编码包括

ASCII text, with CRLF line terminators
Little-endian UTF-16 Unicode English text, with CRLF line terminators
Little-endian UTF-16 Unicode text, with CRLF line terminators
Little-endian UTF-16 Unicode text, with CRLF, CR line terminators
UTF-8 Unicode (with BOM) English text, with CRLF line terminators
UTF-8 Unicode (with BOM) text, with CRLF line terminators

上面的列表是用

生成的
file -b *.csv | sort | uniq

【问题讨论】:

  • 您可以使用第 3 方 chardet 模块在 Python 中进行编码检测。如果你想通过 shell 进行转换,你的系统应该有标准的 chardeticonv 程序。
  • 谢谢。将检查chardet。我之前尝试过iconv,但我发现很难找到file输出和编码的确切名称之间的对应关系。

标签: python csv encoding utf-8


【解决方案1】:

你尝试过写作吗:

import pandas as pd
df=pd.read_csv(file,encoding='utf-8')

【讨论】:

  • 是的,没用,也试过encoding="ISO-8859-1"
猜你喜欢
  • 2013-09-12
  • 2020-10-11
  • 1970-01-01
  • 2011-10-26
  • 2018-01-16
  • 2015-02-17
  • 2021-03-12
  • 2011-05-21
  • 1970-01-01
相关资源
最近更新 更多