考虑引号的字符串拆分

想象一下这个字符串:

"a","b","hi, this is Mboyle"

我想用逗号将其分开,除非逗号在两个引号之间:即:

["a","b","hi, this is Mboyle"]

我该如何实现?使用拆分,“嗨,这是Mboyle”也将拆分!


一只甜甜圈
浏览 169回答 3
3回答

慕村9548890

您可以用逗号分割字符串,而不用逗号分割",":In [1]: '"a","b","hi, this is Mboyle"'.strip('"').split('","')Out[1]: ['a', 'b', 'hi, this is Mboyle']

qq_笑_17

我对这个问题的看法(谨慎使用!)s = '"a","b","hi, this is Mboyle"'new_s = eval(f'[{s}]')print(new_s)输出:['a', 'b', 'hi, this is Mboyle']编辑(更安全的版本):import ast.literal_evals = '"a","b","hi, this is Mboyle"'new_s = ast.literal_eval(f'[{s}]')

阿波罗的战车

解决了。with gzip.open(file, 'rt') as handler:    for row in csv.reader(handler, delimiter=","):
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python