I've working on reading an SPSS file (.sav). My code below can read .sav files. However, I've encounter a very strange error. When I try to read another .sav file, it gives the following error
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.
Here is my code:
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)
The .sav file is running properly on SPSS. However, while using these Python codes, some .sav files don't work (this code works most of other .sav files).
Here is the file you can use: child abilities
Any idea what is going on here? I would appreciate your help.
There is an easier way to read SPSS files into pd.DataFrame(), by using "pd.read_spss(filepath)" method. It works with your file.
import pandas as pd
file_path = "./child_abilities.sav"
df = pd.read_spss(file_path)
Please be noted that you must install Pyreadstat.
% pip install pyreadstst
or
% conda install -c conda-forge pyreadstat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With