Repository navigation
Expand file tree
/
Copy pathremove_line_breaks.py
More file actions
97 lines (80 loc) · 3.65 KB
/
Copy pathremove_line_breaks.py
File metadata and controls
97 lines (80 loc) · 3.65 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
from tqdm import tqdm
import pdb
from multiprocessing import Pool
""" Remove line breaks from malformed CSVs from Pig """
#data_dirpath = '/usr0/home/mamille2/erebor/tumblr/data/sample200/nonreblogs_descs_orig'
#data_dirpath = '/usr2/mamille2/tumblr/data/sample1k/nonreblogs_descs_orig'
data_dirpath = '/usr2/mamille2/tumblr/data/sample1k/reblogs_descs_orig'
out_dirpath = '/usr2/mamille2/tumblr/data/sample1k/reblogs_descs_cleaned'
#csv_fnames = [f for f in sorted(os.listdir(data_dirpath)) if f.startswith('part')]
csv_fnames = sorted(os.listdir(data_dirpath))
#ncols = 51 # nonreblogs
ncols = 60 # reblogs
if not os.path.exists(out_dirpath):
os.mkdir(out_dirpath)
def process_csv(csv_fname):
tqdm.write(csv_fname)
csv_fpath = os.path.join(data_dirpath, csv_fname)
outlines = []
out_fpath = os.path.join(out_dirpath, csv_fname)
# Read in line-by-line, split by '\t'
with open(csv_fpath, 'r') as f:
data = f.read()
#header_idx = data.find('\n')
#header = data[:header_idx].split('\t')
#outlines = [header]
#if len(header) != ncols:
# pdb.set_trace()
#print(header)
#print(ncols)
#tabbed = data[header_idx:].split('\t') # But some segments still break lines with \n
first_segment = data[:data.find('\t')]
tabbed = data[data.find('\t'):].strip().split('\t') # But some segments still break lines with \n
line = [first_segment]
tab_ctr = 0
#pbar = tqdm(total=len(tabbed))
while tab_ctr < len(tabbed):
segment = tabbed[tab_ctr]
if segment.startswith('"'): # quoted column--in case contains \t
# Keep appending segments until segment ends with a "
quote_count = segment.count('"')
while quote_count % 2 != 0:
tab_ctr += 1
#pbar.update(1)
if tab_ctr < len(tabbed):
quote_count += tabbed[tab_ctr].count('"')
segment += ' ' + tabbed[tab_ctr]
else:
break
if len(line) % (ncols-1) == 0: # Ending segment of a line
if tab_ctr == len(tabbed) - 1: # final segment of last line
line.append(segment.replace('\n', ' '))
outlines.append('\t'.join(line))
else:
parts = segment.split('\n')
if len(parts) != 2: # infrequent; skip line
tab_ctr +=1
#pbar.update(1)
line = []
continue
outlines.append('\t'.join(line + [parts[0]])) # first part goes to previous line
line = [parts[1]] # second part goes to new line
else:
line.append(segment.replace('\n', ' '))
tab_ctr += 1
#pbar.update(1)
# Save out outlines with line breaks
with open(out_fpath, 'w') as f:
f.write('\n'.join(outlines))
tqdm.write("Wrote cleaned TSV")
#print()
def main():
for csv_fname in tqdm(csv_fnames):
process_csv(csv_fname)
#map(process_csv, tqdm(csv_fnames))
#with Pool(15) as pool:
# tqdm(pool.imap(process_csv, csv_fnames), total=len(csv_fnames)) # doesn't work right--should fix
#pool.map(process_csv, tqdm(csv_fnames)) # tqdm might not work
if __name__ == '__main__':
main()