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.
關(guān)于line.strip()的用法:用來(lái)去掉文字首尾的whitespace辉巡, 如果沒(méi)有文字,那么去掉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.
這段話說(shuō)明whitespace包括空格,Tab和換行符
line1: [' 1\n', '2 4\n', '\n', '3 \n', '5\n']
line2: ['1', '2 4', '', '3', '5']
line1中的元素.strip()后變成line2, 所以如果是空行,strip()后會(huì)成為[‘’], 任要判斷l(xiāng)en(line)來(lái)去掉