pythonpython-3.xspss-files

How to set Variable Width and Decimals in SPSS with Python?


I'm trying to manipulate some .sav files with SavReaderWriter. What I already have is this:

with savReaderWriter.SavReader(dirIn, ioUtf8 = True) as reader:
    df = pd.DataFrame(reader.all(), columns = [s for s in reader.header])
    varLabels = reader.varLabels
    varTypes = reader.varTypes
    valueLabels = reader.valueLabels
    varWidth = reader.varWids # <------------- This guy
    varMeasure = reader.measureLevels
    varAlignments = reader.alignments
    varColumnWidths = reader.columnWidths
    varMissingValues = reader.missingValues

and:

with SavWriter(savFileName = dirOut,
               varNames = varNames,
               varTypes = varTypes,
               varLabels= varLabels,
               valueLabels = valueLabels,
               measureLevels = varMeasure,
               columnWidths = varColumnWidths,
               alignments = varAlignments,
               missingValues = varMissingValues,
               ioUtf8=True
              ) as writer:

    for record in records:
        writer.writerow(record)

The problem is that I don't know how can I set the Variable Width that I got when reading the sav at fist code, when using the SavWriter part. Does anyone else know what can I do?


Solution

  • I acctually got it working!

    First I had to get the formats when reading the .sav:

    varFormats = reader.formats
    

    Then just add this param when opening the savWriter:

    formats = varFormats
    

    Kind made my way since the docs doesnt help that much, but gave me an idea how the formats works: https://pythonhosted.org/savReaderWriter/index.html#formats