summaryrefslogtreecommitdiff
path: root/aligncsv.py
blob: ce63a98866ba95bfe98d7fd3939147ccab19a9c2 (plain) (blame)
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
#!/usr/bin/python
# this formats a csv file to a serious whitespace intended format.

import os
import sys

tabs=True
additionalspaces = 5

fname = sys.argv[1]
if not os.path.exists(fname):
	print "that file doesn't exist"
	exit(0);

f=open(fname,"r");
lines=f.readlines()
f.close();
length=0

for line in lines:
	length=max(length, len(line.split(",")))

print "# number of entries =",length


#setup text array
textarray=range(len(lines)+1)
for x in range(len(lines)+1):
	textarray[x] = range(length)

for x in range(length):
	textarray[-1][x] = 0

#find the longest entry in each line in each position
for lineno in range(len(lines)):
	sp=lines[lineno].split(",")
	for pieceno in range(len(sp)):
		sp[pieceno] = sp[pieceno].strip() + "," #for the comma add a char
		textarray[-1][pieceno] = max(len(sp[pieceno]),textarray[-1][pieceno])

if tabs:
	#make it divisable by 8 (tabs work then)
	for pieceno in range(length):
		if (textarray[-1][pieceno] %8) !=0:
			textarray[-1][pieceno] = (((textarray[-1][pieceno])/8)*8)+8

for lineno in range(len(lines)):
	sp=lines[lineno].split(",")
	for pieceno in range(length):
		textarray[lineno][pieceno] = ""
		if pieceno<len(sp):
			sp[pieceno]= sp[pieceno].strip()
			if pieceno<len(sp)-1:
				sp[pieceno]= sp[pieceno] + ","

			if (tabs):
				n=(textarray[-1][pieceno]-len(sp[pieceno]))
				textarray[lineno][pieceno] = sp[pieceno]
				if (n%8) != 0:
					textarray[lineno][pieceno] += "\t"*((n/8)+1)
				else:
					textarray[lineno][pieceno] += "\t"*((n/8))
			else:
				n=(textarray[-1][pieceno]-len(sp[pieceno])+additionalspaces)
				textarray[lineno][pieceno] = " "*(n) + sp[pieceno]


fname = sys.argv[2]
if not os.path.exists(fname):
	print "that file doesn't exist"
	exit(0);
else:
	f=open(fname,"w");
	for line in textarray[:-1]:
		for piece in line:
			f.write(piece)
		f.write("\n")
	f.close()