strip()去掉什么

2019-01-24  本文已影响0人  是人也

Documentation:

Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:
The outermost leading and trailing chars argument values are stripped from the string. Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in chars. A similar action takes place on the trailing end.

关于line.strip()的用法:用来去掉文字首尾的whitespace, 如果没有文字,那么去掉whitespace

The most common whitespace characters may be typed via the space bar or the tab key. Depending on context, a line-break generated by the return or enter key may be considered whitespace as well.

这段话说明whitespace包括空格,Tab和换行符

line1: [' 1\n', '2 4\n', '\n', '3 \n', '5\n']
line2: ['1', '2 4', '', '3', '5']

line1中的元素.strip()后变成line2, 所以如果是空行,strip()后会成为[‘’], 任要判断len(line)来去掉

上一篇下一篇

猜你喜欢

热点阅读