6
|
1 #Converter of multiple dta files to one csv
|
|
2 #M.Baak
|
|
3 #13-11-2013
|
|
4 #last update: 13-11-2013
|
|
5
|
|
6 import sys
|
|
7 import os
|
|
8
|
|
9
|
|
10 file_outputname = sys.argv[1]
|
|
11 file_outputname2 = sys.argv[2]
|
|
12 file_name = sys.argv[3:]
|
|
13
|
|
14 def convert_dta(file_name,file_outputname,file_outputname2):
|
|
15 output = open(file_outputname,'w') #output file
|
|
16 output2 = open(file_outputname2, 'w')
|
|
17
|
|
18 for x in range(0,len(file_name)):
|
|
19 read_file = open(file_name[x], 'r') #open file
|
|
20 a = len(read_file.readlines())
|
|
21 read_file2 = open(file_name[x], 'r') # open file second time
|
|
22
|
|
23
|
|
24 numberlandmarks = 0
|
|
25 header = ""
|
|
26
|
|
27 #for loop, coordinates, number of landmarks and name of sample will be stored in csv format
|
|
28 for x in range(0,a):
|
|
29 b = read_file2.readline().strip()
|
|
30 split_tabs = b.split(' ')
|
|
31 number_columns = len(split_tabs)
|
|
32 if x == 0:
|
|
33 header += b.replace(' ', '_') #name of sample
|
|
34 if number_columns == 3: #coordinates
|
|
35 output.write("%f,%f,%f\n"%(float(split_tabs[0]),float(split_tabs[1]),float(split_tabs[2])))
|
|
36 numberlandmarks += 1 # number of landmarks
|
|
37
|
|
38 output2.write("%s\n"%(header[1:-4])) # writing header to output file
|
|
39
|
|
40 output.close()
|
|
41
|
|
42 convert_dta(file_name,file_outputname,file_outputname2)
|
|
43
|
|
44
|
|
45
|
|
46
|