-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat-to-column.py
More file actions
58 lines (41 loc) · 1.42 KB
/
flat-to-column.py
File metadata and controls
58 lines (41 loc) · 1.42 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
'''
Take flat data like:
x,y,z,x,y,z etc
and make
x y z
x y z
x y z
convert a 1D array in row-major order into a 2D array with only 3 columns
you need to specify the number of rows based on the length of the 1D array
and the number of columns
python flat-to-column.py input.csv output.csv 3
'''
import sys
import sys
def main(input_file, output_file, columns):
# Read input file
with open(input_file, 'r') as f:
data = f.read().strip()
# Split the data into individual values
values = data.split(',')
rows = len(values) // columns # Calculate the number of rows based on the length of the array
if len(values) % columns != 0:
raise ValueError("Number of elements in the array is not divisible by the number of columns.")
# Initialize an empty 2D array
arr_2d = [[0] * columns for _ in range(rows)]
# Fill the 2D array with values from the 1D array in row-major order
k = 0
for j in range(rows):
for i in range(columns):
arr_2d[j][i] = values[k]
k += 1
# Write the transposed values to a text file
with open(output_file, 'w') as f:
for row in arr_2d:
f.write('\t'.join(map(str,row)) + '\n') # Separate columns by tabs
if __name__ == "__main__":
args = sys.argv[1:]
input_file = args[0]
output_file = args[1]
columns = int(args[2])
main(input_file, output_file, columns)