473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Fixed width txt to csv

Dear users and helpers,
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:

Expand|Select|Wrap|Line Numbers
  1. import csv
  2. rf = open(r'C:\Users\...New Folder\practice.txt', 'r') #input file handle
  3. wf = open(r'C:\Users\...New Folder\Book1.csv','w') #output file handle
  4. writer = csv.writer(wf)
  5.  
  6. for row in rf.readlines():
  7.     writer.writerow(row.split())
  8. rf.close() # close input file handle
  9. wf.close() # close output file handle
Any help would be greatly appreciated.
Mar 30 '12 #1
2 12390
bvdet
2,851 Expert Mod 2GB
The field widths do not appear to match the data. For example, the sum of the field widths is 315 but line 1 of your data is 131 characters including spaces. Anyway, I would think something like the following would work:
Expand|Select|Wrap|Line Numbers
  1. fieldwidths = [10, 60, 60, 60, 30, 2, 3, 6, 9, 5, 6, 12, 12, 5, 3, 12, 12, 5, 3]
  2. data = """0000000626ISOCKE BBBB ZZZZZ TW DARTMOUTH 10 FDSAF DR DARTMOUTH CASN 7H44DR SAAB -11.111111 22.2222222 000 -33.333333 44.4444444 000
  3. 0000000627ISOCKE FFFF TTTTT TW HALIFAX 3367 FDSAF RD HALIFAX CASN 8C5ASE SAAB -55.555555 66.6666666 000 -77.777777 88.8888888 000
  4. 0000000628ISOCKE RE CHARLOTTETOWN 449 UYRNT ECSARW RD CHARLOTTETOWN CAPE CSE8HR SAAB -99.999999 11.1111111 000 -22.222222 33.3333333 000"""
  5.  
  6. outputList = []
  7.  
  8. for line in data.split("\n"):
  9.     idx = 0
  10.     lineList = []
  11.     for i in fieldwidths:
  12.         upper = idx+i
  13.         lineList.append(line[idx:upper])
  14.         idx = upper
  15.     outputList.append(lineList)
Apr 3 '12 #2
dwblas
626 Expert 512MB
I don't think you can do that with the csv module, but will have to add the comma yourself. Note that there is a discrepancy between the lengths of the output format and the fields of data.
Expand|Select|Wrap|Line Numbers
  1. data=["0000000626ISOCKE BBBB ZZZZZ TW DARTMOUTH 10 FDSAF DR DARTMOUTH CASN 7H44DR SAAB -11.111111 22.2222222 000 -33.333333 44.4444444 000",
  2.  "0000000627ISOCKE FFFF TTTTT TW HALIFAX 3367 FDSAF RD HALIFAX CASN 8C5ASE SAAB -55.555555 66.6666666 000 -77.777777 88.8888888 000",
  3.  "0000000628ISOCKE RE CHARLOTTETOWN 449 UYRNT ECSARW RD CHARLOTTETOWN CAPE CSE8HR SAAB -99.999999 11.1111111 000 -22.222222 33.3333333 000"]
  4.  
  5. fieldwidths = [10, 60, 60, 60, 30, 2, 3, 6, 9, 5, 6, 12, 12, 5, 3, 12, 12, 5, 3]output_format = ["%"+str(x)+"s," for x in fieldwidths]
  6. ## strip the comma on the last element
  7. output_format[-1] = output_format[-1][:-1]
  8. print output_format
  9.  
  10. fp=open("./test1.csv", "w")
  11. for rec in data:
  12.     data_list = rec.split()
  13.     print len(data_list), "-->", len(fieldwidths)
  14.     for offset in range(len(data_list)):
  15.         fp.write(output_format[offset] % data_list[offset])
  16.     fp.write("\n")
  17. fp.close() 
Apr 3 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
2
by: TPSreport | last post by:
ASP.NET 2003. Two DataGrids, six columns each, one grid atop the other. I need to have the columns line up, i.e., DataGrid1 Col(1) lined up w/ DataGrid2 Col(1) - each the same width on the...
179
by: SoloCDM | last post by:
How do I keep my entire web page at a fixed width? ********************************************************************* Signed, SoloCDM
3
by: Doug McCrae | last post by:
http://www.btinternet.com/~doug.mccrae/go4learning/index.html As it says in the subject, a two column layout (plus header and footer boxes). I feel the left column really needs to be fixed width...
0
by: VRandy | last post by:
Example: http://pages.prodigy.net/randyv/centerprb.htm The desired effect is a fixed width left margin with a fixed width div centered in the right side. If the browser is resized to become very...
28
by: Gabriel | last post by:
greetings, I want to achieve the following effect : Menu1 | Menu2 | Menu3 | Menu4 | Menu5 | I played with padding, border-width and the like and it's ok for the | . My problem is that...
5
by: Johnny Meredith | last post by:
I have seven huge fixed width text file that I need to import to Access. They contain headers, subtotals, etc. that are not needed. There is also some corrupt data that we know about and can...
2
by: Oliver | last post by:
Hi all. Using Access 2000. Trying to import fixed width file that has column headings that I want to becom field names - it's not an option :( Is there a way to achieve this? Thanks Oliver
12
by: JB | last post by:
Hi All, Is it acceptable to use a fixed width vertical navigation column within a fluid 2 or 3 column layout? Example. Left Column (navigation) fixed width of say 180px Right Column (main...
4
by: Jeff | last post by:
Hey I'm wondering how the Fixed-Width Text Format is What I know is that the top line in this text format will contain column names. and each row beneath the top line represent for example a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.