使用pandasread_stata()函数时汉字全部乱码
Chinese characters all become gibberish when using pandas read_stata() function
我正在尝试使用 python pandas 包读取 Stata .dta 文件,使用 read_stata() 函数,并且 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*
编解码器(gb18030
、gbk
、gb2312
) 或 UTF 编解码器(UTF-8
、UTF-16
或 UTF-32
)。尽管上面熊猫文档中有评论,但我看到 Stata 14 supports Unicode now,并且他们为此使用 UTF-8。
另请参阅 Standard Encodings page 以了解支持的编解码器的概述。
我正在尝试使用 python pandas 包读取 Stata .dta 文件,使用 read_stata() 函数,并且 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*
编解码器(gb18030
、gbk
、gb2312
) 或 UTF 编解码器(UTF-8
、UTF-16
或 UTF-32
)。尽管上面熊猫文档中有评论,但我看到 Stata 14 supports Unicode now,并且他们为此使用 UTF-8。
另请参阅 Standard Encodings page 以了解支持的编解码器的概述。