February 14th, 2013, 01:39 AM
Extract particular text from a CSV and write it back
I would like to have the output be a new CSV with the original data in row , and then if there is only one number in the row ie the house number that is written to row , if there are 2 numbers in a row (row and row ) then they should be written to row  and  respectively and if there is no number then the string from row  written to row . Ultimately I need to separate flat number, house number and house name into 3 different columns.
So if I have a csv that looks like
I would get a new file with the output
123 Some Street
Flat 1, 21 Other road
House, Someother street
I am new to python so please go easy on me my code so far
123 Some Street, , 123,
Flat 1, 21 Other road, 1, 21,
House, Someother street, , , House.
I know it is not quite right but I am struggling.
csvFile = open(myData.csv, 'rb')
csvOut = open(myOut.csv, 'wb')
reader = csv.reader(csvFile)
writer = csv.writer(csvOut)
for row in reader:
a = row  re.compile('\d+')
a1 = row  re.compile('\d+')
b = row 
b1 = row  re.compile('\d+')
if b = re.compile('\d+')
elif a = re.compile('\d+')
witer.writerow(a,b, , b1, )
writer.writerow(a,b, , ,a)
February 14th, 2013, 09:06 AM
Use the interpreter
Please spend some time trying your code as well as example code in the python interpreter.
After you've compiled a regular expression use that object's search, match, or other methods to perform a desired action.
Last edited by b49P23TIvg; February 14th, 2013 at 09:10 AM.
[/code] are essential for python code and Makefiles!