SavWriter mode="cp" -> problem :(
Hello,
I am working with savReaderWriter to change some data inside of all my sav files (around 2000 files).
My 1st step in my script is to open the input file sav and copy all the data and metadata and write the exact new file.( after i could do this, i will be able to change the data)
Unfortunatly, i tried few things to keep the metadata but none of them is fully working. I tried : - open my file with SavHeaderReader and use
metadata = header.dataDictionary(asNamedtuple=False)
to be albe to use
**metadata
in my writer. But unicodeError. https://bitbucket.org/fomcl/savreaderwriter/issues/30/problem-with-special-characters-in-python2
-I tried to use mode="cp" but I think i misused it and had an error
File "helloyou/savReaderWriter/generic.py", line 194, in openSavFile
with open(savFileName, mode) as f:
ValueError: mode string must begin with one of 'r', 'w', 'a' or 'U', not 'cp'
-For now I try this
with spss.SavWriter('changed_'+path+'/DONE_'+file_name+"."+extension_file, overwrite=True, ioUtf8=True,varNames=metadata.varNames, varTypes=metadata.varTypes,
varLabels=metadata.varLabels, valueLabels=metadata.valueLabels, columnWidths=metadata.columnWidths,
missingValues=metadata.missingValues, caseWeightVar=metadata.caseWeightVar, measureLevels=metadata.measureLevels,
fileAttributes=metadata.fileAttributes) as writer:
for i, record in enumerate(records):
writer.writerow(record)
It's terrible but it working but i still lost 2 metadata (width and decimals)
Do you have ideas ?! :)
I submitted a patch for this problem some time ago, although it was never incorporated. https://bitbucket.org/fomcl/savreaderwriter/issues/59/possible-issue-with-savwriter-mode-cp
Also mentioned on that PR is the fact that you have to specify varNames and varTypes, which I feel should be pulled from the source file on a copy operation.