I want to convert multiple text files into a single csv file.the text are named as(file1.txt,file2.txt....file1000.txt). A text file(file1.txt) format is as follows:
Employee id: us51243
Employee name: Mark santosh
department:engineering
Age:25
I want the ouput as:
Employee id,Employee name,department,Age
us51243,Mark santosh,engineering,25//(file1.txt values)
...................................//(file2.txt values)
But in the ouput I am getting the value of file1000.txt only as follows:
Employee id,Employee name,department,Age
us98621,Andy Gonzalez,Support & services,25
Here is my code:
import csv
import os
for x in range(1,1001):
filepath=os.path.normpath('C:\\Text\\file{}.txt'.format(x))
with open(filepath) as f, open('emp.csv', 'w',newline='') as file:
writer = csv.writer(file)
val = zip(*[l.rstrip().split(': ') for l in f])
writer.writerows(val)
Kindly note:Also I want to display the header(Employee id,Employee name,Department,Age) only once