[Tutor] process and modify a list of strings, in place

Mark Tolonen metolone+gmane at gmail.com
Sat Feb 12 05:49:53 CET 2011


"John Martinetti" <smed at missinglynx.net> wrote in message 
news:AANLkTi=8KvksqzpUJSQwFFJ8YaRjCWHuHaWTzkzFVNV6 at mail.gmail.com...
> Hello -
>
> I'm a novice programmer, not even amateur level and I need some help with
> developing an algorithm to process a list of strings.
> I hope this list is tolerant of n00bs, if not, please let me know and I'll
> take this elsewhere.
>
>
> Some background.
>
> I cut up a text based report from a reporting application into fields by
> slicing the lines up using fixed column widths to delimit the fields. 
> This
> was necessary to ensure that a couple of the fields that had free-form
> "comments" with variable spacing.  If there were well-defined field
> delimiters I could have used awk or some other text processing tool. 
> Python
> worked very well for cutting up the lines from the report into fields and
> let me store each "record" into a list of strings.  So - I have a list of
> openPOs which is a list of strings.
> I do this like this:
>
> #############################################################################################################
> #! /usr/bin/python
> import sys, re
> txtreport=open("open_pos.txt",'r')
>
> openPOs=[]
>
> while 1:
>
>   record = txtreport.readline() # - reads in a line of data from the 
> report
> file
>
>   vendornum = record[:6]       # - breaks out each column of the report
> into fields
>   vendorname = record[7:40]
>   ordernum = record[41:47]
>   ordersuffix = record[48:59]
>   orderdate = record[60:70]
>   buyer = record[71:77]
>   partnum = record[78:100]
>   qty = record[101:108]
>   comment = record[109:135]
>
>   # - create a record from all the fields
>   linedata = (vendornum, vendorname, ordernum, ordersuffix, orderdate,
> buyer, partnum, qty, comment)
>   # - append the record to the list of records representing the CQ report
>   openPOs.append(linedata)
>
>   # if not the end of the file, do it again
>   if not record:
>      break

I noticed when splitting up your record line there is a character skipped 
between each field.   A delimiter perhaps?  Then you may be interested in 
the csv module.  Assuming the delimiter is '|', the following code will 
produce the same result in openPOs:

    import csv
    with open('open_pos.txt','rb') as f:
        reader = csv.reader(f,delimiter='|')
        openPOs = list(reader)
    print openPOs

Note you may have to specify some more parameters to csv.reader depending on 
your file "dialect".  See the csv.Dialect class for details.

-Mark




More information about the Tutor mailing list