[Tutor] modify csv textfile
Alan Gauld
alan.gauld at btinternet.com
Sat Aug 7 13:26:34 CEST 2010
"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?
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list