在python中格式化无序字符串

Formatting an unordered string in python

我有一根绳子,就像…

1
"1""2""3""4""""text1""text2""text3""6""first && second && third.."""" 7""         8"..

我的目标是…

1
 1,2,3,4,,text1,text2,text3,6,first && second && third..,,7,8,..

有人能帮我吗?


如果您有一个字符串列表,那么去掉每个空白元素,并使用str.join()将这些元素与分隔符连接在一起:

1
','.join([s.strip() for s in list_of_strings])

演示:

1
2
3
>>> 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,'