I have csv file, which information (id and text) in row's looks like in example below:
Field1:
Text:
1, A,A,A,B
Field2:
Text:
2, A,B,C,C
My desired output:
Field1: Field2: Field1: Field2:
ID: Text: ID: Text:
1 A 2 A
1 A 2 B
1 A 2 C
1 B 2 C
How can i do that via python ? Thanks !
CodePudding user response:
Here is a way to do what your question asks:
with open('infoo.txt', 'r', encoding="utf-8") as f:
records = []
rows = [[x.strip() for x in row.split(',')] for row in f.readlines()]
i = 0
while i < len(rows):
gotOneOrMoreRows = False
if len(rows[i]) == 1 and rows[i][0].startswith('Field'):
gotOneOrMoreRows = True
i = 1
if i < len(rows) and len(rows[i]) == 1 and rows[i][0] == 'Text:':
gotOneOrMoreRows = True
i = 1
row = rows[i]
records.append([[row[0], row[i]] for i in range(1, len(row))])
if not gotOneOrMoreRows:
i = 1
with open('outfoo.txt', 'w', encoding="utf-8") as g:
tab = '\t'
g.write(f'{tab.join(["Field 1:" tab "Field 2:" for record in records])}\n')
g.write(f'{tab.join(["ID:" tab "Text:" for record in records])}\n')
for i in range(len(records[0])):
g.write(f'{tab.join(colPair[i][0] tab colPair[i][1] for colPair in records)}\n')
# check the output file:
with open('outfoo.txt', 'r', encoding="utf-8") as f:
print('contents of output file:')
[print(row.strip('\n')) for row in f.readlines()]
Output:
contents of output file:
Field 1: Field 2: Field 1: Field 2:
ID: Text: ID: Text:
1 A 2 A
1 A 2 B
1 A 2 C
1 B 2 C
Note that this kind of thing may be easier using pandas.