早上好
我正在尝试使用Python中的SPSS文件(.sav)
这是我的代码:
import pandas as pd
df=pd.read_spss('C:/Users/bonif/Documents/CSALUD01.sav')
df.head()
我得到这个错误:
df=pd.read_spss('C:/Users/bonif/Documents/CSALUD01.sav')
File "C:\Users\bonif\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\spss.py", line 44, in read_spss
df, _ = pyreadstat.read_sav(
File "pyreadstat\pyreadstat.pyx", line 342, in pyreadstat.pyreadstat.read_sav
File "pyreadstat\_readstat_parser.pyx", line 1034, in pyreadstat._readstat_parser.run_conversion
File "pyreadstat\_readstat_parser.pyx", line 845, in pyreadstat._readstat_parser.run_readstat_parser
File "pyreadstat\_readstat_parser.pyx", line 775, in pyreadstat._readstat_parser.check_exit_status
pyreadstat._readstat_parser.ReadstatError: Unable to convert string to the requested encoding (invalid byte sequence)
我发现这个错误可能是因为有些单词带有字母“ñ”,或者有些单词带有下面的字符“á”。我该如何解决这个问题
数据库在这个google驱动器中:https://drive.google.com/drive/folders/1P8v5NWE-GdAEJRZdmrp5KiL-DODClmfU?usp=sharing
多谢各位
Pandas调用
pyreadstat
读取SPSS文件src直接使用它可能会更幸运,因为它有一个设置编码的选项
从文件https://github.com/Roche/pyreadstat#other-options
也可能是您根本没有安装iconv(它依赖于iconv进行编码),但我对此表示怀疑(您可能会遇到其他错误)
正如ti7所建议的,使用pyreadstat,您需要指定编码,在本例中,latin1将完成以下操作:
相关问题 更多 >
编程相关推荐