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

John Martinetti smed at missinglynx.net
Thu Feb 10 22:52:12 CET 2011


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
########################################################################################################################


All of the records (each one is type list) has a field (of type string) that
represents the "buyer ID" (the report is about purchase orders).  Due to the
crappy ERP application we have, there are Purchase Orders in the system with
no "buyer ID" (six spaces).  Obviously, this is a problem.
I need to be able to scan each record in, detect whether field index #5  is
blank and has no buyer ID, and if so, change that field to be "NOBUYER"
instead of blank (six spaces actually) so that some further processing down
the road can pick up on that and handle it appropriately.
I can reliably detect the field as being blank using the following code:

for line in openPOs:  ### openPOs is  of type 'list'
     if line[5] == "      ":
         print "The buyerID field is blank!!!!"


I can run this code after the initial routine that chops up the text based
report and stores it into the list "openPOs" and reliably detect each
"record"(list) has a blank "buyerID" field (which is field #5 as you can
tell by my slice).

The part I'm having a problem with is once I've detected that a record has a
blank buyerID field, I can't seem to figure out how to change it, in place.
I've tried finding the index of the openPOs using this:
openPOs.index(line)
and then trying to change the item like this:
openPOs[openPOs.index(line),5] = "NOBUYER"
but I'm getting a strange error:  "TypeError: 'tuple' object does not
support item assignment"

I've thrown in a bunch of debugging code to "type" each variable I'm
using...the "line" variable in the code above is the only tuple, but I can't
for the life of me figure out what I'm doing wrong.

And now I've been staring at it too long and I'm getting myself confused.
Perhaps I'm going about changing this field in this list of strings all the
wrong way....I'm somewhat familiar with indexing a multi-dimensional array
in other languages to search for certain values and replace them with
something else, but I'm kinda stumped on this one.

Any thoughts would be appreciated.  I can share more code but I need to
sanitize it so nothing confidential gets out, please let know what would be
helpful.
Thanks -

smed
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110210/689af321/attachment-0001.html>


More information about the Tutor mailing list