I have a text file which looks like this image:
I would like to convert it to the excel format using python. once done it will look like this image
]2
Please guide me how to proceed.
I have a text file which looks like this image:
I would like to convert it to the excel format using python. once done it will look like this image
]2
Please guide me how to proceed.
The following should work:
import csv
directions = ["***LEFT", "***RIGHT", "***UPPER", "***BOTTOM", "***Average"]
with open(r"foo.txt", "r") as f:
data = f.readlines()
with open(r"foo.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["SN"] + ["Item"] + [line.split()[-1] for line in data[::6]])
writer.writerow(["C23ABC"] + ["Position"] + [" ".join(line.split()[:-1]) for line in data[::6]])
for i in range(5):
writer.writerow([""] + [directions[i]] + [line.split()[0] for line in data[i+1::6]])
However, spaces or leading zeroes can cause problems.