想象一下这个字符串:"a","b","hi, this is Mboyle"我想用逗号将其分开,除非逗号在两个引号之间:即:["a","b","hi, this is Mboyle"]我该如何实现?使用拆分,“嗨,这是Mboyle”也将拆分!
3 回答
慕村9548890
TA贡献1884条经验 获得超4个赞
您可以用逗号分割字符串,而不用逗号分割",":
In [1]: '"a","b","hi, this is Mboyle"'.strip('"').split('","')
Out[1]: ['a', 'b', 'hi, this is Mboyle']
qq_笑_17
TA贡献1818条经验 获得超7个赞
我对这个问题的看法(谨慎使用!)
s = '"a","b","hi, this is Mboyle"'
new_s = eval(f'[{s}]')
print(new_s)
输出:
['a', 'b', 'hi, this is Mboyle']
编辑(更安全的版本):
import ast.literal_eval
s = '"a","b","hi, this is Mboyle"'
new_s = ast.literal_eval(f'[{s}]')
阿波罗的战车
TA贡献1862条经验 获得超6个赞
解决了。
with gzip.open(file, 'rt') as handler:
for row in csv.reader(handler, delimiter=","):
添加回答
举报
0/150
提交
取消