Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a Python string with nested separated symbol

I need from string

i = "1,'Test','items (one, two, etc.)',1,'long, list'"

extract array of next strings:

['1', "'Test'", "'items (one, two, etc.)'", '1', "'long, list'"]

with help of regexpress

r=re.split(r',+(?=[^()]*(?:\(|$))', i)

I receive next result only:

['1', "'Test'", "'items (one, two, etc.)'", '1', "'long", " list'"]

UPD1

NULL should be supported

i = "1,'Test',NULL,'items (one, two, etc.)',1,'long, list'"
['1', "'Test'", 'NULL', "'items (one, two, etc.)'", '1', "'long, list'"]
like image 633
constructor Avatar asked Apr 22 '15 20:04

constructor


People also ask

How do you split with multiple separators?

Use the String. split() method to split a string with multiple separators, e.g. str. split(/[-_]+/) . The split method can be passed a regular expression containing multiple characters to split the string with multiple separators.

How do you separate symbols in Python?

Python split() method is used to split the string into chunks, and it accepts one argument called separator. A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default.

Can you split with multiple characters in Python?

split() This is the most efficient and commonly used method to split multiple characters at once. It makes use of regex(regular expressions) in order to do this.


2 Answers

You don't need re.split in this case.you can use re.findall within a list comprehension :

>>> [k for j in re.findall(r"(\d)|'([^']*)'",i) for k in j if k]
['1', 'Test', 'items (one, two, etc.)', '1', 'long, list']

The preceding regex will match any thing between one quote '([^']*)' or any digit (\d).

Or as a more efficient way in this case you can use ast.literal_eval :

>>> from ast import literal_eval
>>> literal_eval(i)
(1, 'Test', 'items (one, two, etc.)', 1, 'long, list')
like image 174
Mazdak Avatar answered Oct 11 '22 20:10

Mazdak


This is a task for the csv module:

import csv
from StringIO import StringIO
line = "1,'Test','items (one, two, etc.)',1,'long, list'"
reader = csv.reader(StringIO(line), quotechar="'")
row = next(reader)

# row == ['1', 'Test', 'items (one, two, etc.)', '1', 'long, list']

The key here is to create a CSV reader, specifying single quote as the quote character.

like image 25
Hai Vu Avatar answered Oct 11 '22 20:10

Hai Vu