格式化Python列表

时间:2018-07-11 12:04:43

标签: python list format

您好,我目前正在从网上抓取信息并将其放入列表中,但是元素显示为:

['\n      Michael\n    ', 
'\n      Elad\n    ', 
'\n      Yair\n    ',  
'\n      Liron\n    ',  
'\n      Idan\n    ',  
'\n      Tomer\n    ',  
'\n      Ofer\n    ',  
'\n      Asaf\n    ',  
'\n      Tomer\n    ',  
'\n      Ronit\n    ',  
'\n      Guy\n    ',  
'\n      Lior\n    ']

是否可以格式化列表以消除行缩进和空白?

谢谢

1 个答案:

答案 0 :(得分:0)

使用.strip()方法:

>>> l = ['\n      Michael\n    ', '\n      Elad\n    ']
>>> l
['\n      Michael\n    ', '\n      Elad\n    ']
>>> [x.strip() for x in l]
['Michael', 'Elad']