1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Rep Power

    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 [0],[1] and then if there is only one number in the row ie the house number that is written to row [3], if there are 2 numbers in a row (row[0] and row [1]) then they should be written to row [2] and [3] respectively and if there is no number then the string from row [0] written to row [4]. 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

    123 Some Street
    Flat 1, 21 Other road
    House, Someother street
    I would get a new file with the output

    123 Some Street, , 123, 
    Flat 1, 21 Other road, 1, 21,
    House, Someother street, , , House.
    I am new to python so please go easy on me my code so far

    import csv
    import re
    csvFile = open(myData.csv, 'rb')
    csvOut = open(myOut.csv, 'wb')
    reader = csv.reader(csvFile)
    writer = csv.writer(csvOut)
    for row in reader:
        a = row [0] re.compile('\d+')
        a1 = row [0] re.compile('\d+')
        b = row [1]
        b1 = row [1] re.compile('\d+')
            if b = re.compile('\d+')
                writer.writerow(a,b,a1,b1, )
            elif a = re.compile('\d+')
                witer.writerow(a,b, , b1, )
                writer.writerow(a,b, , ,a)
    I know it is not quite right but I am struggling.

    Many thanks
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power

    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]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo