-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.py
More file actions
76 lines (60 loc) · 1.66 KB
/
split.py
File metadata and controls
76 lines (60 loc) · 1.66 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#-----------------------------------------------------------------------
# split.py
#-----------------------------------------------------------------------
import sys
import stdarray
from instream import InStream
from outstream import OutStream
#-----------------------------------------------------------------------
# Accept string fileName and integer fieldCount as command-line
# arguments. Split the file whose name is fileName.csv, by field,
# into fieldCount+1 files named fileName1.txt, fileName2.txt, etc.
DELIM = ','
fileName = sys.argv[1]
fieldCount = int(sys.argv[2])
# Create the input stream.
inStream = InStream(fileName + '.csv')
# Create output streams.
outStreams = stdarray.create1D(fieldCount)
for i in range(fieldCount):
file = OutStream(fileName + str(i) + '.txt')
outStreams[i] = file
# Read lines from the input stream and write them to the
# output stream.
while inStream.hasNextLine():
line = inStream.readLine()
fields = line.split(DELIM)
for i in range(fieldCount):
outStreams[i].writeln(fields[i])
#-----------------------------------------------------------------------
# more djia.csv
# Date,Open,High,Low,Close,Volume,Adj. Close*
# 17-Mar-06,11294.94,11294.94,11253.23,11279.65,2549619968,11279.65
# 16-Mar-06,11210.97,11324.80,11176.07,11253.24,2292179968,11253.24
# 15-Mar-06,11149.76,11258.28,11097.23,11209.77,2292999936,11209.77
# ...
# python split.py djia 3
# more djia0.txt
# Date
# 17-Mar-06
# 16-Mar-06
# 15-Mar-06
# ...
# more djia1.txt
# Open
# 11294.94
# 11210.97
# 11149.76
# ...
# more djia2.txt
# High
# 11294.94
# 11324.80
# 11258.28
# ...
# more djia3.txt
# Low
# 11253.23
# 11176.07
# 11097.23
# ...