我有一个像......这样的字符串。
"1" "2" "3" "4" " " "text1" "text2" "text3" "6" "first && second && third.." " " " 7 " " 8"..
我打算实现的是......
1,2,3,4,,text1,text2,text3,6,first && second && third..,,7,8,..
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:1)
如果你有一个列表字符串,那么去除空格的每个元素并使用str.join()
将它们与分隔符连接起来:
','.join([s.strip() for s in list_of_strings])
演示:
>>> list_of_strings = ["1", "2", "3", "4", " ", "text1", "text2", "text3", "6", "first && second && third..", " ", " 7 ", " "]
>>> ','.join([s.strip() for s in list_of_strings])
'1,2,3,4,,text1,text2,text3,6,first && second && third..,,7,'