使用Python3.x读取文件时不跳过空行? 我试图读取一个文件,然后跳过空白行。出于某种原因,它并没有真正跳过空行。我做错了什么 ourFile = 'File.txt' with open(ourFile) as fp: for tmpLine in fp: currentLine = tmpLine.strip() if currentLine != '\n' and currentLine != '\r\n': print(currentLine)
去除所有空白,包括换行符使用Python3.x读取文件时不跳过空行? 我试图读取一个文件,然后跳过空白行。出于某种原因,它并没有真正跳过空行。我做错了什么 ourFile = 'File.txt' with open(ourFile) as fp: for tmpLine in fp: currentLine = tmpLine.strip() if currentLine != '\n' and currentLine != '\r\n': print(currentLine),python,python-3.x,Python,Python 3.x,去除所有空白,包括换行符'\n'或回车符'\r': currentLine = tmpLine.strip() if currentLine != '': print(currentLine) # or simply: if currentLine: print(currentLine) 对于默认情况下字符被剥离的大致方向,您可以查看: 去除所有空白,包括换行符'\n'或回车符'\r': currentLine = tmpLine.strip() if currentLine
'\n'
或回车符'\r'
:
currentLine = tmpLine.strip()
if currentLine != '':
print(currentLine)
# or simply:
if currentLine:
print(currentLine)
对于默认情况下字符被剥离的大致方向,您可以查看:
去除所有空白,包括换行符'\n'
或回车符'\r'
:
currentLine = tmpLine.strip()
if currentLine != '':
print(currentLine)
# or simply:
if currentLine:
print(currentLine)
对于默认情况下字符被剥离的大致方向,您可以查看:
.strip()
默认情况下删除\r\n
字符(在无参数调用时)。因此,如果currentLine:Ev.Konis=>您的解决方案有效,请检查。如何关闭记录单并将您的答案标记为解决方案?请将答案标记为@schwobasegl。它完全相同。.strip()
默认情况下删除\r\n
字符(在无参数调用时)。因此,如果currentLine:
Ev.Konis=>您的解决方案有效,请检查。如何关闭记录单并将您的答案标记为解决方案?请将答案标记为@schwobasegl。完全一样。如果currentLine:
就足够了。@cᴏʟᴅsᴘᴇᴇᴅ 我正要加上:)想先把OP拿到他所在的地方。如果currentLine:
就足够了。@cᴏʟᴅsᴘᴇᴇᴅ 我刚想补充一句:)我想先把OP拿到他所在的地方。