-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathxpt2csv.py
40 lines (32 loc) · 1.21 KB
/
xpt2csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'''
Converts all the XPT files in the specified folder to csv. It
uses R for the conversion, through the RPy2 interface:
http://rpy.sourceforge.net/rpy2_documentation.html
To install rpy2, run from the command line:
easy_install rpy2
The Hmisc package should be installed in R:
install.packages("Hmisc") (from R)
@copyright: Fathom Information Design 2014
'''
import rpy2.robjects as robjects
import sys, os
xpt_dir = sys.argv[1]
csv_dir = sys.argv[2]
if not os.path.exists(csv_dir):
os.makedirs(csv_dir)
print("Converting XPT files to CSV...")
robjects.r("library(Hmisc)")
files = os.listdir(xpt_dir)
for f in files:
xpt_fn = os.path.abspath(os.path.join(xpt_dir, f))
name_full = os.path.split(f)[1]
(name_base, name_ext) = os.path.splitext(name_full)
if name_ext.lower() == '.xpt':
# Importing XPT file into a data frame and exporting as csv
robjects.r('file_data <- sasxport.get("' + xpt_fn + '")')
csv_fn = os.path.abspath(os.path.join(csv_dir, name_base.upper() + ".csv"))
try:
robjects.r('write.csv(file_data, file = "' + csv_fn + '", row.names = FALSE)')
except:
print("Error converting " + xpt_fn + " to CSV, skipping.")
print("Done.")