我不能去掉空格和换行符。你知道哪里出了问题吗?
line_count = 0
word_count = 0
for fline in fh:
line = repr(fline)
line = line.strip()
print line
line_count += 1
word_count += len(line.split())
result['size'] = filesize
result['line'] = line_count
result['words'] = word_count
输出'value of $input if it isn'
' larger than or equal to ygjhgn'
' that number. Otherwise assigns the value of n'
' n'
' '
由于repr()
:
>>> x = 'hellon'
>>> repr(x)
"'hello\n'"
>>> repr(x).strip()
"'hello\n'"
>>>
这是你编辑的代码:
line_count = 0
word_count = 0
for fline in fh:
line = repr(line.strip())
print line
line_count += 1
word_count += len(line.split())
result['size'] = filesize
result['line'] = line_count
result['words'] = word_count
如果fline
是一个字符串,那么用它作为参数调用repr
将把它括在字面引号中。因此:
foon
是
"foon"
由于换行符不再位于字符串的末尾,strip
将不会删除它。也许可以考虑不调用repr
,除非你迫切需要,或者在调用strip
后调用。
从别人提到的,只要改变
line = repr(fline)
line = line.strip()
line = line.strip()
line = repr(fline)
请注意,您可能需要.rstrip()
甚至.rstrip("n")
。