想象一下这个字符串:
"a","b","hi, this is Mboyle"
除非逗号在两个引号之间,否则我想将其分割为逗号: 即:
["a","b","hi, this is Mboyle"]
我该如何实现?使用拆分,“嗨,这是Mboyle”也将拆分!
答案 0 :(得分:3)
您可以使用","
而不是逗号来分隔字符串:
In [1]: '"a","b","hi, this is Mboyle"'.strip('"').split('","')
Out[1]: ['a', 'b', 'hi, this is Mboyle']
答案 1 :(得分:2)
我对这个问题的看法(请谨慎使用!)
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}]')
答案 2 :(得分:1)
已解决。
with gzip.open(file, 'rt') as handler:
for row in csv.reader(handler, delimiter=","):
这很有趣!谢谢大家
答案 3 :(得分:1)
您可以在拆分中包括引号,因此使用.split('","')
。然后根据需要删除最终清单项目上的引号。
答案 4 :(得分:1)
您可以使用re.split
:
import re
s = '"a","b","hi, this is Mboyle"'
new_s = list(map(lambda x:x[1:-1], re.split('(?<="),(?=")', s)))
输出:
['a', 'b', 'hi, this is Mboyle']
但是,re.findall
更干净:
new_result = re.findall('"(.*?)"', s)
输出:
['a', 'b', 'hi, this is Mboyle']