My file is like this Heading1, Heading2, Heading3, Heading4, Heading5
Alpha, Beta, Gama, Face, Human
Dog, Camel, Horse, Lion
################# Summary ###############################
JavaScriptx71Heading6 = V8CA02TB
2Heading7 = 8R1
3Heading8 = ENVS2F12SLB2
4Heading9 = mkern2
5Heading10 = 1654774930624
6Heading11 = 09 13:42:10 2022
7
I want to make file like this
Heading1, Heading2, Heading3, Heading4, Heading5, Heading6, Heading7, Heading8, Heading9, Heading10, Heading11
Alpha, Beta, Gama, Face, Human, V8CA02TB, 8R1, ENVS2F12SLB2, mkern2, 1654774930624, 09 13:42:10 2022
Cat, Dog, Camel, Horse, Lion, V8CA02TB, 8R1, ENVS2F12SLB2, mkern2, 1654774930624, 09 13:42:10 2022
I have to do that on multiple files and then combine all the files in one text file
Any help will be appreciated.
Advertisement
Answer
This will parse your format.
JavaScript
1
28
28
1
with open('x.data') as fin:
2
header = None
3
rows = []
4
extra = []
5
6
for line in fin:
7
if len(line) < 5:
8
continue
9
if not header:
10
header = line.strip().split(',')
11
elif 'Summary' in line:
12
break
13
else:
14
rows.append( line.strip().split(',') )
15
16
for line in fin:
17
line = line.strip()
18
if not line:
19
continue
20
left,_,right = line.partition(' = ')
21
header.append( left )
22
extra.append( right )
23
24
with open('out.data', 'w') as fout:
25
print( ','.join(header), file=fout )
26
for row in rows:
27
print( ','.join(row+extra), file=fout )
28
Output:
JavaScript
1
4
1
A,B,C,D,E,F,G,H,I,J,K
2
Alpha, Beta, Gama, Face, Human,V8CA02TB,8R1,ENVS2F12SLB2,mkern2,1654774930624, 09 13:42:10 2022
3
Cat, Dog, Camel, Horse, Lion,V8CA02TB,8R1,ENVS2F12SLB2,mkern2,1654774930624, 09 13:42:10 2022
4
Combining multiple files is easy, even without Python.