Source

savReaderWriter / savReaderWriter / doc_tests / test_SavWriter_mode_cp.txt

##############################################################################
## Use a nested context manager and mode="cp"
##############################################################################

>>> from __future__ import print_function
>>> import os
>>> from savReaderWriter import *

###############
## Copy header info from one file to another (Method #1)
>>> savFileName = "../savReaderWriter/test_data/Employee data.sav"
>>> with SavHeaderReader(savFileName) as spssDict:
...     wholeDict = spssDict.dataDictionary()  # doctest: +ELLIPSIS
...
>>> with SavReader(savFileName) as reader:
...     with SavWriter("test_wholeDict.sav", **wholeDict) as writer:
...         for line in reader:
...             writer.writerow(line)

# clean up
>>> try:
...     os.remove(os.path.abspath("test_wholeDict.sav"))
... except:
...     pass

###############
## Copy header info from one file to another (Method #2)
## Uses <refSavFileName> as a donor file to initialize the header
## This will also copy DATE/TREND info, which is not copied using Method #1
>>> savFileName = "../savReaderWriter/test_data/Employee data.sav"
>>> with SavReader(savFileName, rawMode=True) as reader:
...     varNames, varTypes = reader.getSavFileInfo()[2:4]
...     with SavWriter("Employee data_out.sav", varNames, varTypes,
...                    refSavFileName=savFileName, mode="cp") as writer:
...         for line in reader:
...             writer.writerow(line)

# read the data back in to see if it looks the same
>>> with SavReader("Employee data_out.sav", returnHeader=True) as reader:  # doctest: +ELLIPSIS
...     header = reader.next()
...     print(header)
...     for line in reader:
...         print(line)
['id', 'gender', 'bdate', 'educ', 'jobcat', 'salary', 'salbegin', 'jobtime', 'prevexp', 'minority']
[1.0, 'm', '1952-02-03', 15.0, 3.0, 57000.0, 27000.0, 98.0, 144.0, 0.0]
[2.0, 'm', '1958-05-23', 16.0, 1.0, 40200.0, 18750.0, 98.0, 36.0, 0.0]
[3.0, 'f', '1929-07-26', 12.0, 1.0, 21450.0, 12000.0, 98.0, 381.0, 0.0]
...
[462.0, 'f', '1963-10-18', 16.0, 3.0, 34410.0, 19500.0, 65.0, 79.0, 0.0]
[463.0, 'f', '1934-10-15', 15.0, 1.0, 20700.0, 14250.0, 65.0, 241.0, 0.0]
[464.0, 'm', '1962-03-20', 19.0, 3.0, 47550.0, 33000.0, 64.0, 27.0, 0.0]
[465.0, 'm', '1962-07-20', 12.0, 1.0, 33900.0, 16500.0, 64.0, 106.0, 0.0]
[466.0, 'f', '1948-06-15', 12.0, 1.0, 23400.0, 13500.0, 64.0, 198.0, 0.0]
[467.0, 'f', '1967-08-18', 16.0, 1.0, 32850.0, 19500.0, 64.0, 20.0, 0.0]
[468.0, 'f', '1965-11-28', 16.0, 3.0, 55750.0, 19980.0, 64.0, 36.0, 0.0]
[469.0, 'f', '1964-06-01', 15.0, 1.0, 25200.0, 13950.0, 64.0, 57.0, 0.0]
[470.0, 'm', '1964-01-22', 12.0, 1.0, 26250.0, 15750.0, 64.0, 69.0, 1.0]
[471.0, 'm', '1966-08-03', 15.0, 1.0, 26400.0, 15750.0, 64.0, 32.0, 1.0]
[472.0, 'm', '1966-02-21', 15.0, 1.0, 39150.0, 15750.0, 63.0, 46.0, 0.0]
[473.0, 'f', '1937-11-25', 12.0, 1.0, 21450.0, 12750.0, 63.0, 139.0, 0.0]
[474.0, 'f', '1968-11-05', 12.0, 1.0, 29400.0, 14250.0, 63.0, 9.0, 0.0]

# read the meta data back in to see if it looks the same
>>> with SavHeaderReader("Employee data_out.sav") as header:  # doctest: +ELLIPSIS
...     print(str(header))
...
#ALIGNMENTS
salary -- right
jobcat -- right
bdate -- right
minority -- right
prevexp -- right
gender -- left
salbegin -- right
jobtime -- right
educ -- right
id -- right
#CASEWEIGHTVAR
#COLUMNWIDTHS
salary -- 8
jobcat -- 8
bdate -- 13
minority -- 8
prevexp -- 8
gender -- 1
salbegin -- 8
jobtime -- 8
educ -- 8
id -- 8
#FILEATTRIBUTES
#FILELABEL
05.00.00
#FORMATS
salary -- DOLLAR8
jobcat -- F1
bdate -- ADATE10
minority -- F1
prevexp -- F6
gender -- A1
salbegin -- DOLLAR8
jobtime -- F2
educ -- F2
id -- F4
#MEASURELEVELS
salary -- ratio
jobcat -- ordinal
bdate -- ratio
minority -- ordinal
prevexp -- ratio
gender -- nominal
salbegin -- ratio
jobtime -- ratio
educ -- ordinal
id -- ratio
#MISSINGVALUES
salary: values -- 0.0
jobcat: values -- 0.0
minority: values -- 9.0
salbegin: values -- 0.0
jobtime: values -- 0.0
educ: values -- 0.0
#MULTRESPDEFS
#VALUELABELS
salary: 0.0 -- missing
jobcat: 0.0 -- 0 (Missing)
jobcat: 1.0 -- Clerical
jobcat: 2.0 -- Custodial
jobcat: 3.0 -- Manager
salbegin: 0.0 -- missing
minority: 0.0 -- No
minority: 1.0 -- Yes
minority: 9.0 -- 9 (Missing)
prevexp: 0.0 -- missing
gender: f -- Female
gender: m -- Male
jobtime: 0.0 -- missing
educ: 0.0 -- 0 (Missing)
educ: 8.0 -- 8
educ: 12.0 -- 12
educ: 14.0 -- 14
educ: 15.0 -- 15
educ: 16.0 -- 16
educ: 17.0 -- 17
educ: 18.0 -- 18
educ: 19.0 -- 19
educ: 20.0 -- 20
educ: 21.0 -- 21
#VARATTRIBUTES
#VARLABELS
salary -- Current Salary
jobcat -- Employment Category
bdate -- Date of Birth
minority -- Minority Classification
prevexp -- Previous Experience (months)
gender -- Gender
salbegin -- Beginning Salary
jobtime -- Months since Hire
educ -- Educational Level (years)
id -- Employee Code
#VARNAMES
id
gender
bdate
educ
jobcat
salary
salbegin
jobtime
prevexp
minority
#VARROLES
salary -- input
jobcat -- input
bdate -- input
minority -- input
prevexp -- input
gender -- input
salbegin -- input
jobtime -- input
educ -- input
id -- input
#VARSETS
SALARY -- salbegin, salary
DEMOGR -- gender, minority, educ
#VARTYPES
salary -- 0
jobcat -- 0
bdate -- 0
minority -- 0
prevexp -- 0
gender -- 1
salbegin -- 0
jobtime -- 0
educ -- 0
id -- 0

# clean up
>>> try:
...     os.remove(os.path.abspath("Employee data_out.sav"))
... except:
...     pass 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.