如何从 Python 中的列表中删除 \n\t\r?
How to remove \n\t\r from list in Python?
代码:
list = ["\r\n Opportunities\r\n ", "\r\n Careers\r\n ", "\r\n Education & training\r\n ", "\r\n Find Your Future\r\n ","\r\n\tSanderson Recruitment, Solutions, Executive, Consulting\r\n"]
我想要的输出:
["Opportunities","Careers","Education & training","Find Your Future","Sanderson Recruitment, Solutions, Executive, Consulting"]
因为是列表
您似乎想从字符串中去除空格。您可以使用列表理解将 strip
方法应用于列表的每个元素。
mylist = ["\r\n Opportunities\r\n ",
"\r\n Careers\r\n ",
"\r\n Education & training\r\n ",
"\r\n Find Your Future\r\n ",
"\r\n\tSanderson Recruitment, Solutions, Executive, Consulting\r\n"
]
newlist = [s.strip() for s in mylist]
您可以从字符串中去除周围的空格
out = [x.strip() for x in l]
代码:
list = ["\r\n Opportunities\r\n ", "\r\n Careers\r\n ", "\r\n Education & training\r\n ", "\r\n Find Your Future\r\n ","\r\n\tSanderson Recruitment, Solutions, Executive, Consulting\r\n"]
我想要的输出:
["Opportunities","Careers","Education & training","Find Your Future","Sanderson Recruitment, Solutions, Executive, Consulting"]
因为是列表
您似乎想从字符串中去除空格。您可以使用列表理解将 strip
方法应用于列表的每个元素。
mylist = ["\r\n Opportunities\r\n ",
"\r\n Careers\r\n ",
"\r\n Education & training\r\n ",
"\r\n Find Your Future\r\n ",
"\r\n\tSanderson Recruitment, Solutions, Executive, Consulting\r\n"
]
newlist = [s.strip() for s in mylist]
您可以从字符串中去除周围的空格
out = [x.strip() for x in l]