ありがとうございました新しいregex
モジュールの:
import regex as re
string = 'a,b,c,"d,e, g,",f'
rx = re.compile(r'"[^"]*"(*SKIP)(*FAIL)|,')
parts = rx.split(string)
print(parts)
# ['a', 'b', 'c', '"d,e, g,"', 'f']
それは、この例では、二重引用符betweemすべて無視(*SKIP)(*FAIL)
メカニズムをサポートしています。
あなたは二重引用符をエスケープしている場合は、使用できます。
import regex as re
string = '''a,b,c,"d,e, g,",f, this, one, with "escaped \"double",quotes:""'''
rx = re.compile(r'".*?(?<!\\)"(*SKIP)(*FAIL)|,')
parts = rx.split(string)
print(parts)
# ['a', 'b', 'c', '"d,e, g,"', 'f', ' this', ' one', ' with "escaped "double",quotes:""']
はregex101.com上、後者のデモを参照してください。ほぼ50ポイントの場合
は、私も
csv
方法を提供することで感じる:
import csv
string = '''a,b,c,"d,e, g,",f, this, one, with "escaped \"double",quotes:""'''
# just make up an iterable, normally a file would go here
for row in csv.reader([string]):
print(row)
# ['a', 'b', 'c', 'd,e, g,', 'f', ' this', ' one', ' with "escaped "double"', 'quotes:""']
複数のオプションが用意されている場合はどうしてですか? http://stackoverflow.com/a/34528938/1560062 – zero323