[Tutor] modify csv textfile

David Hutto smokefloat at gmail.com
Sat Aug 7 14:27:37 CEST 2010


On Sat, Aug 7, 2010 at 8:26 AM, David Hutto <smokefloat at gmail.com> wrote:
> On Sat, Aug 7, 2010 at 7:26 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>>
>> "TGW" <galaxywatcher at gmail.com> wrote
>>>
>>> What I want to output is:
>>> 12345|some text|some more text|example|example32423
>>> 11223|more text|and more|example|example455667
>>>
>>> So column 4 is where the change occurs, but only if the beginning
>>> of the string in column 4  =~ /^example/i  # and it should be case
>>> insensitive
>>>
>>
>>> reader = csv.reader(open(filename, 'rb'), delimiter='|',
>>> quoting=csv.QUOTE_NONE)
>>> for row in reader:
>>>   print row
>>>
>>> ....
>>> I can print the file, I just need a little help searching and replacing
>>> the column 4 data element.
>>
>> OK, so I'm not sure which bit is confusing you.
>> The reader returns a list of fields per row.
>> You want the fourth column which is element 3 in row - ie. row[3]
>> You can use startswith() or a regex to test the value
>> You can replace the string with whatever you like since lists are mutable
>> You can then store/write the modified list to whatever/wherever you like.
>>
>> Now which bit of that is causing you grief?
>
> Probably the explanation. Mainly because of lack of documentation than
> google terms,
Not to say that docs are laccking to the proportion of want.

and appropriate questions
>>
>> --
>> Alan Gauld
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>


More information about the Tutor mailing list