-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt2csv.py
40 lines (35 loc) · 1.42 KB
/
txt2csv.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
import csv
txt_file = r"activityLabel.txt"
csv_file = r"activityLabel.csv"
# use 'with' if the program isn't going to immediately terminate
# so you don't leave files open
# the 'b' is necessary on Windows
# it prevents \x1a, Ctrl-z, from ending the stream prematurely
# and also stops Python converting to / from different line terminators
# On other platforms, it has no effect
in_txt = csv.reader(open(txt_file, "rt"), delimiter = ',')
out_csv = csv.writer(open(csv_file, 'wt', newline = ""))
out_csv.writerows(in_txt)
activityID = []
with open('activityLabel.csv', 'rt', newline="") as f:
reader = csv.reader(f)
# read file row by row
for row in reader:
s = row
activityID.append(row[0])
del activityID[-1]
for j in range(0,len(activityID)):
txt_file = activityID[j]+'.txt'
print(txt_file)
csv_file = activityID[j]+'.csv'
#txt_file = r"activityLabel.txt"
#csv_file = r"activityLabel.csv"
in_txt = csv.reader(open(txt_file, "rt"), delimiter = ' ')
out_csv = csv.writer(open(csv_file, 'wt', newline=""))
out_csv.writerows(in_txt)
# use 'with' if the program isn't going to immediately terminate
# so you don't leave files open
# the 'b' is necessary on Windows
# it prevents \x1a, Ctrl-z, from ending the stream prematurely
# and also stops Python converting to / from different line terminators
# On other platforms, it has no effect