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

John Martinetti smed at missinglynx.net
Fri Feb 11 18:25:38 CET 2011


Dave -

Thanks so much for your feedback.
You pointed out the biggest error that I made, I was unable to see the
mistake myself, but yes...I stored the "record" in the list "openPOs" as a
tuple.  Doh!  That's not what I meant to do.  I most certainly meant to make
that a list, because one of the design specs from the beginning on this
project would be that I am going to need to modify all of the contents of
the list "openPOs"
Thank you very much for pointing that out to me, I'm going to correct that
first thing.
Also - I appreciate your suggestions for modifying the buyerID field in
place, that is the most desired result for me.  I got all tangled up around
the axle trying to modify it indirectly...that's what happens when you stare
at the terminal too long, I'm sure you've been there.

Again - Thanks a bunch, I'm going to modify my code and I'll get back to the
list if I have any other issues that I need help with.

Lastly - this list is great...I think I'm going to learn a lot here.
Thanks.



On Thu, Feb 10, 2011 at 5:52 PM, Dave Angel <davea at ieee.org> wrote:

> On 01/-10/-28163 02:59 PM, John Martinetti wrote:
>
>> Hello -
>>
>>  Welcome,
>
>
>> <snip>
>>
>>    # - 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)
>>
>>  (snip)
>
>
>> 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"
>>
>>
>>
> You assign a tuple to linedata with the (vendornum, vendorname, ...)
> syntax.  Change those to square brackets and it'll be a list, which is
> modifiable.
>
> There are other ways to simplify your code, but the most relevant might be
> to modify line[5] directly:
>   if line[5] == "      " :
>       print "The field is blank!!!"
>       line[5] = "NOBUYER"
>
> So first change it so that individual "line"s are lists instead of tuples.
> Then change the list item directly, without resorting to searching with
> index().
>
> Another thing I note:  You're counting on the file ending with a blank
> line.  If you're sure that'll always be the case, fine.  But if you really
> just want to read the whole file, replace the while True loop with something
> like:
>
> for record in txtreport:
>
> and if you need to ignore blank lines, use
>    if record="" : continue
>
> --
> --
> davea at ieee.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110211/ab006595/attachment.html>


More information about the Tutor mailing list