gpt4 book ai didi

python - 在Python中读取SPSS(.sav)文件时出现 "title already used as a name or title"错误

转载 作者:行者123 更新时间:2023-12-03 16:26:50 24 4
gpt4 key购买 nike

我正在研究读取SPSS文件(.sav)。我下面的代码可以读取.sav文件。但是,我遇到了一个非常奇怪的错误。当我尝试读取另一个.sav文件时,出现以下错误

Traceback (most recent call last):
File "C:\Users\fatihshen\Documents\Merjek
Project\Predictive_Analytics\sav_reader.py", line 28, in <module>
read_spss_file(file_path)
File "C:\Users\fatihshen\Documents\Merjek
Project\Predictive_Analytics\sav_reader.py", line 10, in read_spss_file
records = reader.all()
File "C:\Users\fatihshen\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\savReaderWriter\savReaderNp.py", line 541, in all
return self.to_structured_array(filename)
File "C:\Users\fatihshen\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\savReaderWriter\savReaderNp.py", line 122, in _convert_datetimes
array = func(self, *args)
File "C:\Users\fatihshen\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\savReaderWriter\savReaderNp.py", line 148, in _convert_missings
array = func(self, *args)
File "C:\Users\fatihshen\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\savReaderWriter\savReaderNp.py", line 531, in to_structured_array
array = np.fromiter(self, self.trunc_dtype, self.nrows)
File "C:\Users\fatihshen\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\savReaderWriter\helpers.py", line 17, in fget_memoized
setattr(self, attr_name, fget(self))
File "C:\Users\fatihshen\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\savReaderWriter\savReaderNp.py", line 376, in trunc_dtype
return np.dtype(obj)
ValueError: title already used as a name or title.

这是我的代码:
import savReaderWriter as spss
import pandas as pd

my_df = None

def read_spss_file(file_name):
global my_df
with spss.SavReaderNp(file_name) as reader:
records = reader.all()
my_df = pd.DataFrame(records)

file_path = "dataset/child_abilities.sav"
read_spss_file(file_path)
print(my_df)

.sav文件在SPSS上正常运行。但是,在使用这些Python代码时,某些.sav文件不起作用(此代码在大多数其他.sav文件中均有效)。

这是您可以使用的文件:
child abilities

知道这里发生了什么吗?多谢您的协助。

最佳答案

通过使用“pd.read_spss(filepath)”方法,有一种更简单的方法可以将SPSS文件读入pd.DataFrame()。它适用于您的文件。

import pandas as pd

file_path = "./child_abilities.sav"
df = pd.read_spss(file_path)

请注意,您必须安装Pyreadstat。
% pip install pyreadstst
or
% conda install -c conda-forge pyreadstat

关于python - 在Python中读取SPSS(.sav)文件时出现 "title already used as a name or title"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49660528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com