-2

我有一个像...这样的字符串

"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,..  

谁能帮我这个 ?

4

1 回答 1

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,'
于 2013-09-19T07:42:54.767 回答