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

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -