且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在换行符处拆分字符串

更新时间:2023-11-13 22:14:58

如果你只关心尾随的换行符,你可以这样做:

If you are concerned only with the trailing newline, you can do:

a.rstrip().split('\n')

参见 str.lstrip() 和 str.strip() 以了解变化.

See, str.lstrip() and str.strip() for variations.

如果您更普遍地担心多余的换行符会产生空项目,您可以这样做:

If you are more generally concerned by superfluous newlines producing empty items, you can do:

filter(None, a.split('\n'))