regex - Query CSV and write original CSV and results to single CSV Python -



regex - Query CSV and write original CSV and results to single CSV Python -

i trying parse csv , if criteria met in either column write out new csv.

for example

if have csv looks like

123 street flat 1, 21 other road house, someother street

i need analyse each line if number appears in first column , not sec need extract number, if there number in both columns need extract both , if there no number need extract text in first column. write new csv 2 original columns , 3 new ones number 1, number 2, text. ie flat number, house number, house name. new csv like

123 street, , 123, flat 1, 21 other road, 1, 21, house, someother street, , , house.

any guidance helpful.

thanks

edited

import csv csvfile = 'mydata.csv' csvout = 'myout.csv' reader = csv.reader(csvfile) author = csv.writer(csvout) row in reader: num = \d | \d\d | \d\d\d if row [0] || row [1] == num if row [1] == num writer.row [3] else row [0] == num writer.row [2] writer.row [3] else writer.row [0] [2] csvout.close()

edited again

i hope might clearer explination:

i have output new csv original info in row [0],[1] , if there 1 number in row ie house number written row [3], if there 2 numbers in row (row[0] , row [1]) should written row [2] , [3] respectively , if there no number string row [0] written row [4]. need separate flat number, house number , house name 3 different columns.

a farther edit

i have been working on code , have following, sense getting closer still way away?

import csv import re csvfile = open(mydata.csv, 'rb') csvout = open(myout.csv, 'wb') reader = csv.reader(csvfile) author = csv.writer(csvout) row in reader: = row [0] re.compile('\d' | '\d\d' | '\d\d\d') a1 = row [0] re.compile('\d' | '\d\d' | '\d\d\d') b = row [1] b1 = row [1] re.compile('\d' | '\d\d' | '\d\d\d') if b = re.compile('\d' | '\d\d' | '\d\d\d') writer.writerow(a,b,a1,b1, ) elif = re.compile('\d' | '\d\d' | '\d\d\d') witer.writerow(a,b, , b1, ) else writer.writerow(a,b, , ,a) csvout.close()

thanks

this might give clue i'm not wholly sure need.

$cat t1

123 street flat 1, 21 other road house, 23 someother street

example

import csv import re p = re.compile('\d+') row in csv.reader(open('t1')): print "row", row match = p.search(row[0]) if match: print "\t#1", match.group() if len(row) > 1: match = p.search(row[1]) if match: print "\t#2", match.group()

output

row ['123 street'] #1 123 row ['flat 1', ' 21 other road'] #1 1 #2 21 row ['house', ' 23 someother street'] #2 23

python regex parsing csv

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -