从字符串中删除额外的逗号

时间:2016-07-21 15:18:31

标签: python-2.7

我无法从以下字符串列表中删除额外的逗号

[',156,151,2016-06-07',',160,147,2016-03-16',',99,91,2016-06-11']

我尝试使用加入和删除,但它没有工作

期待如下结果

['156,151,2016-06-07','160,147,2016-03-16','99,91,2016-06-11']

请帮助我......提前谢谢

3 个答案:

答案 0 :(得分:1)

您可以使用列表理解:

>>> l = [',156,151,2016-06-07',',160,147,2016-03-16',',99,91,2016-06-11']
>>> l = [i.lstrip(',') for i in l]
>>> l
['156,151,2016-06-07', '160,147,2016-03-16', '99,91,2016-06-11']

答案 1 :(得分:0)

只需使用str.replace

newArr = []
for line in arr:
    newArr.append(line.replace(',', '')

答案 2 :(得分:0)

你可以使用列表理解: -

>>> l= [',156,151,2016-06-07',',160,147,2016-03-16',',99,91,2016-06-11']
>>> [i.lstrip(',') for i in l]
['156,151,2016-06-07', '160,147,2016-03-16', '99,91,2016-06-11']