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'"]
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.
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.
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.
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')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With