Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use python to convert a fixed width txt file to csv?

I searched online, but could not find the answer to my question. I need convert a txt file to a csv. I have figured out how to do this with delimeters, however, the txt file does not have any delimeters or headers so I must set fixed width numbers. The file has millions of records. The widths for the columns are 10, 60, 60, 60, 30, 2, 3, 6, 9, 5, 6, 12, 12, 5, 3, 12, 12, 5, 3.

My two challenges are: 1. To convert the file to csv with the fixed widths listed above. 2. To insert headers.

The data looks something like this:

0000000626ISOCKE BBBB ZZZZZ TW DARTMOUTH 10 FDSAF DR DARTMOUTH CASN 7H44DR SAAB -11.111111 22.2222222 000 -33.333333 44.4444444 000
0000000627ISOCKE FFFF TTTTT TW HALIFAX 3367 FDSAF RD HALIFAX CASN 8C5ASE SAAB -55.555555 66.6666666 000 -77.777777 88.8888888 000
0000000628ISOCKE RE CHARLOTTETOWN 449 UYRNT ECSARW RD CHARLOTTETOWN CAPE CSE8HR SAAB -99.999999 11.1111111 000 -22.222222 33.3333333 000

Again, all I've been able to is convert the file to csv but not in the correct format using this code:

import csv
rf = open(r'C:\Users\...New Folder\practice.txt', 'r') #input file handle
wf = open(r'C:\Users\...New Folder\Book1.csv','w') #output file handle
writer = csv.writer(wf)

for row in rf.readlines():
    writer.writerow(row.split())
rf.close() # close input file handle
wf.close() # close output file handle
like image 377
talpol00 Avatar asked Dec 21 '22 23:12

talpol00


1 Answers

Use struct to tear each fixed-width row apart, trimming where appropriate.

like image 160
Ignacio Vazquez-Abrams Avatar answered Mar 29 '23 23:03

Ignacio Vazquez-Abrams