最后写了很多:
lines = [l.strip() for l in lines]
Run Code Online (Sandbox Code Playgroud)
是否有一种可读的Pythonista方式:lines.stripped()它返回剥离的行?(在C#中,您可以在字符串列表中添加"扩展方法").
不,你不能monkeypatch这种list类型.您可以list使用这样的方法创建子类,但这可能是一个糟糕的主意.
Pythonic捕获重复代码的方法是编写一个函数:
def stripped(strings):
return [s.strip() for s in strings]
Run Code Online (Sandbox Code Playgroud)