繁体   English   中英

使用python 3.x读取文件不会跳过空行?

[英]Reading a file using python 3.x not skipping blank lines?

我正在尝试读取文件,只是跳过空白行。 由于某种原因,它并没有真正跳过空白行。 我究竟做错了什么?:

ourFile = 'File.txt'
with open(ourFile) as fp:
    for tmpLine in fp:
        currentLine = tmpLine.strip()
        if currentLine != '\n' and currentLine != '\r\n':
             print(currentLine)

strip()去除所有空格,包括换行符'\\n'或回车符'\\r'

currentLine = tmpLine.strip()
if currentLine != '':
    print(currentLine)
# or simply:
if currentLine:
    print(currentLine)

对于默认情况下会去除字符的粗略方向,您可以查看string.whitespace

import string
string.whitespace
# '\t\n\x0b\x0c\r '

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM