我下载了一个文件和文件的字符串,如下所示: “124.5”, “108.5”, “115”, “115.5”, “114”, “115”
我可以拆分字符串使它看起来像这样: 124.5,108.5,115,115.5,114,115
用逗号分隔它
非常感谢!!
答案 0 :(得分:1)
>>> s = '"124.5","108.5","115","115.5","114","115"'
>>> ','.join([item.replace('"', '') for item in s.split(',')])
'124.5,108.5,115,115.5,114,115'
item.replace('"', '')
:从item
s.split(',')
:返回['"124.5", "108.5", ... ]
item in s.split(',')
:每个字符串(例如,'" 124.5"')','.join([...])
:将其重新组合为逗号分隔的字符串显然,如果你不做最后一步(','.join
),你可以将它们作为一个列表。
答案 1 :(得分:0)
只需删除引号:
x = x.replace('"', '')
答案 2 :(得分:0)
>>> s = '"124.5","108.5","115","115.5","114","115"'
>>> nums = [float(x) for x in s.replace('"', '').split(',')]
>>> nums
[124.5, 108.5, 115, 115.5, 114, 115]
答案 3 :(得分:0)
这应该可以解决问题:
','.join(s[1:-1] for s in '"124.5","108.5","115","115.5","114","115"'.split(','))