[Tutor] modify csv textfile
TGW
galaxywatcher at gmail.com
Sat Aug 7 13:45:16 CEST 2010
> You can test if one item in your list begins with example like:
> ' example125 oo3 3456'.lstrip()[:7].lower() == 'example'
I think I need to use regex here. Perhaps you will agree.
Input file:
1119|Avail|53|Potato Chips
1234|Avail|53|Potato Chips and salse
1399|Avail|53|potato chips
1445|Avail|64|Pretzels
1490|Avail|64|Pretzels and mustard
etc...
#!/usr/bin/env python
import csv
import re
filename = raw_input("Enter the filename to edit: ")
reader = csv.reader(open(filename, 'rb'), delimiter='|', quoting=csv.QUOTE_NONE)
for row in reader:
if re.match('potato chips.*', row[4].lower()):
row[4] = 'Potato Chips'
if re.match('^pretzels.*', row[4].lower()):
row[4] = 'Pretzels'
print row
Row will be variable length, so strip() will not work for me. But neither does my latest attempt ^^
More information about the Tutor
mailing list