-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranspose_table.py
50 lines (38 loc) · 1.32 KB
/
transpose_table.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#########################################
# Mini-Tool to transpose a table writte #
# in LeTeX – requires plain table as a #
# file for input #
# #
# (C) 2021 P. Bielefeldt #
# Licensed as GPLv3 or, at your wish, #
# a later version of GPL. #
#########################################
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("in_name", type=str, help="file path")
args = parser.parse_args()
in_name = args.in_name
out_name = args.in_name+"_trans"
table = []
with open(in_name, "r") as in_file:
for line in in_file:
line = line.strip() # strip off any trailing whitespace and the end-of-line
line = line.replace("\\", "") # remove the 'new line' from LaTeX manually
table.append(line.split("& ")) # todo: might not perfectly preserve empty spaces at begin/end of line
# todo: test all rows have same number of columns
#print("table")
#for r in table:
# print(r)
transposed = map(list, zip(*table))
#print("transposed")
#for r in transposed:
# print(r)
out_file = open(out_name, "w") # caution: overwrites file ...
for row in transposed:
line = " & ".join(row)
line = line+" \\\\ \n"
out_file.write(line)
out_file.close()
print("output name is %s" % out_name)