python - 使用pandas read_stata() 函数时汉字都变成乱码

标签 python pandas stata

我正在尝试使用 read_stata() 函数读取带有 python pandas 包的 Stata .dta 文件,并且 dta 文件中有很多汉字。读入的文件全是乱码,汉字全是乱码。有什么建议吗?

最佳答案

您需要指定要使用的编解码器,默认情况下会将您的文本解码为 ISO-8859-1 (Latin-1):

pandas.read_stata(filename, encoding=codec_to_use)

参见 pandas.read_stata() documenation :

encoding: string, None or encoding
Encoding used to parse the files. Note that Stata doesn’t support unicode. None defaults to iso-8859-1.

对于中文,我猜测使用的编解码器是gb*编解码器(gb18030gbkgb2312)或 UTF 编解码器(UTF-8UTF-16UTF-32 ).尽管上面 Pandas 文档中有评论,但我看到了 Stata 14 supports Unicode now ,并且他们为此使用 UTF-8。

另见 Standard Encodings page了解支持的编解码器概览。

关于python - 使用pandas read_stata() 函数时汉字都变成乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31913732/

相关文章:

Python psycopg2 : Copy result of query to another table

latex - 使用 frmttable 以日期格式显示汇总统计信息

stata - "too many variables specified"使用全局宏时出错

python - Pandas 创建新列,其中另一列中的所有条目对应于唯一值

python - 通过迭代替换 Pandas DataFrame 中的数组值

loops - 生成一个新宏,为旧宏的单词添加前缀

带有对 mpirun 的嵌入式调用的 Python

python - Tensorflow 解析 CSV 迭代器按行移位

python - 替换列表中的字符

python - Pandas 聚合有序点坐标