【发布时间】:2018-09-14 13:51:41
【问题描述】:
我正在阅读一个 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
知道这里发生了什么吗?非常感谢您的帮助。
【问题讨论】:
-
我在使用 df.to_records() 时出现名称错误,这是因为我的数据中有重复的列。也许您的数据中有重复项?