0

I have the following (sample data)

name,phone,"addres 1, address 2",foo,bar

i have come across a kind of similar problem (with solution) here: Python Regex - Find numbers with comma in string

what i want is to do a split and have the array look like

['name', 'phone', 'addres 1, address 2' ,'foo', 'bar']

im not brilliant with regex's so was hoping someone can help

Community
  • 1
  • 1
DrogoNevets
  • 1,456
  • 3
  • 18
  • 34
  • 7
    Use the `csv` module, not regular expressions. – Wooble Sep 17 '12 at 13:11
  • 2
    This is not a mixed format CSV. This is standard, valid CSV, that any library should support: http://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules_and_examples – Kobi Sep 17 '12 at 13:18

1 Answers1

6

This is not a regular-expression-shaped nail, so please put down that hammer. Python has a csv module for data like that instead.

>>> from cStringIO import StringIO
>>> import csv
>>> data = StringIO('name,phone,"addres 1, address 2",foo,bar')
>>> for row in csv.reader(data):
...     print row
... 
['name', 'phone', 'addres 1, address 2', 'foo', 'bar']
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343