简体   繁体   中英

How do you extract content between , and Parenthesis(if present) in a csv ROW, in Python

The content of the csv is as follows:

"Washington-Arlington-Al, DC-VA-MD-WV  (MSAD)"  47894  1976
"Grand-Forks, ND-MN"                            24220  2006
"Abilene, TX"                                   10180  1977

The output required is read through the csv, find the content between "" in column 1 and fetch only DC-VA-MD-WV , ND-MN , TX and put this content in a new column. (For Normalization)

So far tried a lot of regex patterns in python, but could not get the right one.

sample=""" "Washington-Arlington-Al, DC-VA-MD-WV  (MSAD)",47894,1976
           "Grand-Forks, ND-MN",24220,2006
           "Abilene, TX",10180,1977  """
 open('sample.csv','w').write(sample)
 with open('sample.csv') as sample, open('output.csv','w') as output:
    reader = csv.reader(sample)
    writer = csv.writer(output)
    for comsplit in row[0].split(','):
        writer.writerow([ comsplit, row[1]])
    print open('output.csv').read()

Output Expected is:

DC-VA-MD-WV
ND-MN
TX

in a new row

I'd do it like this:

with open('csv_file.csv', 'r') as f_in, open('output.csv', 'w') as f_out:
    csv_reader = csv.reader(f_in, quotechar='"', delimiter=',',
                            quoting=csv.QUOTE_ALL, skipinitialspace=True)
    csv_writer = csv.writer(f_out)
    new_csv_list = []
    for row in csv_reader:
        first_entry = row[0].strip('"')
        relevant_info= first_entry.split(',')[1].split('  ')[0]
        row += [relevant_info]
        new_csv_list += [row]
    for row in new_csv_list:
        csv_writer.writerow(row)

Let me know if you have any questions.

There is no need to use regex here provided a couple of things:

  1. The city (?) always has a comma after it followed by 1 space of whitespace (though I could add a modification to accept more than 1 bit of whitespace if needed)
  2. There is a space after your letter sequence before encountering something like (MSAD) .

This code gives your expected output against the sample input:

with open('sample.csv', 'r') as infile, open('expected_output.csv', 'wb') as outfile:
    reader = csv.reader(infile)
    expected_output = []
    for row in reader:
        split_by_comma = row[0].split(',')[1]
        split_by_space = split_by_comma.split(' ')[1]
        print split_by_space   
        expected_output.append([split_by_space])

    writer = csv.writer(outfile)
    writer.writerows(expected_output)

I believe you could use this regex pattern, which will extract any alphanumeric expression (with hyphen or not) between a comma and a parenthesis:

import re
BETWEEN_COMMA_PAR = re.compile(ur',\s+([\w-]+)\s+\(')
test_str = 'Washington-Arlington-Al, DC-VA-MD-WV  (MSAD)'
result = BETWEEN_COMMA_PAR.search(test_str)
if result != None:
    print result.group(1)

This will print as a result: DC-VA-MD-WV , as expected.

It seems that you are having troubles finding the right regex to use for finding the expected values.

I have created a small sample pythext which will satisfy your requirement.

Basically, when you check the content of every value of the first column, you could use a regex like /(TX|ND-MN|DC-VA-MD-WV)/

I hope this was useful! Let me know if you need further explanations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM