I am trying to split a string by commas ","
For example:
"hi, welcome" I would like to produce ["hi","welcome"]
however:
"'hi,hi',hi" I would like to produce ["'hi,hi'","hi"]
"'hi, hello,yes','hello, yes','eat,hello'" I would like to produce ["'hi, hello,yes'","'hello, yes'","'eat,hello'"]
"'hiello, 332',9" I would like to produce ["'hiello, 332'","9"]
I dont think the .split()
function could be used, Does anyone know a way I could do this, perhaps with regex?
You can use the csv module with the quotechar
argument, or you can convert your inputs to use the more standard "
character for their quote character.
>>> import csv
>>> from cStringIO import StringIO
>>> first=StringIO('hi, welcome')
>>> second=StringIO("'hi,hi',hi")
>>> third=StringIO("'hi, hello,yes','hello, yes','eat,hello'")
>>> fourth=StringIO("'hiello, 332',9")
>>> rfirst=csv.reader(first,quotechar="'")
>>> rfirst.next()
['hi', ' welcome']
>>> rsecond=csv.reader(second,quotechar="'")
>>> rsecond.next()
['hi,hi', 'hi']
>>> rthird=csv.reader(third,quotechar="'")
>>> rthird.next()
['hi, hello,yes', 'hello, yes', 'eat,hello']
>>> rfourth=csv.reader(fourth,quotechar="'")
>>> rfourth.next()
['hiello, 332', '9']
>>> second=StringIO('"hi,hi",hi') # This will be more straightforward to interpret.
>>> r=csv.reader(second)
>>> r.next()
['hi,hi', 'hi']
>>> third=StringIO('"hi, hello,yes","hello, yes","eat,hello"')
>>> r=csv.reader(third)
>>> r.next()
['hi, hello,yes', 'hello, yes', 'eat,hello']
With regex, as you asked for:
import re
>>>pattern = re.compile(r"([^',]+,?|'[^']+,?')")
>>>re.findall(pattern, "hi, welcome")
['hi', 'welcome']
>>>re.findall(pattern, "'hi, hello,yes','hello, yes','eat,hello'")
["'hi, hello,yes'", "'hello, yes'", "'eat,hello'"]
>>>re.findall(pattern, "'hi,hi',hi")
["'hi,hi'", 'hi']
>>>re.findall(pattern, "'hiello, 332',9")
["'hiello, 332'", '9']
The first part of the pattern, [^',]+,?
, catches segments without quotes and without commas. It might have a comma at the end or it might not (if it's the last segment).
The second part, '[^']+,?'
, catches segments that are enclosed by quotes. It should not have more quotes internally, but it may have commas.
you could use a csv reader with ,
as delimiter and '
as quotechar. that seems to be compatible with what you expect.
Doing this directly without csv
or re
isn't that problematic:
def splitstring(s):
result = []
for i, piece in enumerate(s.split("'")):
if piece:
if i % 2: # odd pieces are between quotes
result.append("'" + piece + "'")
else: # even pieces aren't
for subpiece in piece.split(","):
if subpiece:
result.append(subpiece)
return result
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.