-
Notifications
You must be signed in to change notification settings - Fork 150
/
split.py
66 lines (44 loc) · 915 Bytes
/
split.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
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
'''
split a file into two randomly, line by line.
Usage: split.py <input file> <output file 1> <output file 2> [<probability of writing to the first file>]'
'''
import csv
import sys
import random
input_file = sys.argv[1]
output_file1 = sys.argv[2]
output_file2 = sys.argv[3]
try:
P = float( sys.argv[4] )
except IndexError:
P = 0.9
try:
seed = sys.argv[5]
except IndexError:
seed = None
try:
skip_headers = sys.argv[6]
except IndexError:
skip_headers = False
try:
skip_headers = sys.argv[6]
except IndexError:
skip_headers = False
print "P = %s" % ( P )
if seed:
random.seed( seed )
i = open( input_file )
o1 = open( output_file1, 'wb' )
o2 = open( output_file2, 'wb' )
if skip_headers:
i.readline()
counter = 0
for line in i:
r = random.random()
if r > P:
o2.write( line )
else:
o1.write( line )
counter += 1
if counter % 100000 == 0:
print counter