在python中按","拆分

use*_*159 1 python regex string

我试图用逗号分隔字符串","

例如:

"hi, welcome"  I would like to produce ["hi","welcome"]
Run Code Online (Sandbox Code Playgroud)

然而:

"'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"]
Run Code Online (Sandbox Code Playgroud)

我不认为这个.split()功能可以使用,有没有人知道我可以这样做的方式,也许是正则表达式?

koj*_*iro 16

您可以将csv模块与quotechar参数一起使用,或者您可以转换输入以使用更标准的"字符作为其引号字符.

>>> 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']
Run Code Online (Sandbox Code Playgroud)

  • 在这里使用csv可能是正确的想法; 但你实际上可以告诉csv使用什么引号字符,因此不需要使用双引号而不是单引号.请参阅<http://docs.python.org/library/csv.html> (5认同)