python - CSV read/write using regex re.sub -


i'd read csv file , recompile using:

re.sub('\s+(street|st|trail|trl|tr)\s*$', '', test_file, flags=re.m) 

i'm getting:

typeerror: expected string or buffer  

when using:

import csv reader = csv.reader(open("some.csv", "rb")) row in reader:     print row  import csv writer = csv.writer(open("some.csv", "wb")) writer.writerows(someiterable) 

looks need function. have sugestions?

you have pass string re.sub() third argument, can line of reader object. can pass writer iterable substitutions:

import csv reader = csv.reader(open("input.csv", "rb")) writer = csv.writer(open("output.csv", "wb")) test = '\s+(street|st|trail|trl|tr)\s*$' writer.writerows( (re.sub(test, '', line[0], flags=re.m) line in reader) ) 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -