您可以将 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']