Python 2.7使用if ifif来浏览文本文件

Mik*_*Lee 1 python if-statement

目标是编写一个脚本,该脚本将复制文本文件并排除以#开头的任何行.

我的问题是我似乎得到一个索引错误,这取决于我的if ifif条件的顺序.非工作代码和工作代码之间的唯一区别(除了后缀"_bad"到非工作函数名称)是我首先测试""条件(工作)与首先测试"#"条件(不起作用)

基本文件由此脚本创建:

>>> testFileObj = open("test.dat","w")  
>>> testFileObj.write("#line one\nline one\n#line two\nline two\n")  
>>> testFileObj.close()
Run Code Online (Sandbox Code Playgroud)

有效的代码:

def copyAndWriteExcludingPoundSigns(origFile, origFileWithOutPounds):    
    origFileObj = open(origFile,"r")  
    modFileObj = open(origFileWithOutPounds,"w")  
    while True:  
        textObj = origFileObj.readline()    
        if textObj == "":    
            break    
        elif textObj[0] == "#":    
            continue    
        else:    
            modFileObj.write(textObj)    
    origFileObj.close()    
    modFileObj.close()    
Run Code Online (Sandbox Code Playgroud)

代码不起作用:

def copyAndWriteExcludingPoundSigns_Bad(origFile, origFileWithOutPounds):  
    origFileObj = open(origFile,"r")  
    modFileObj = open(origFileWithOutPounds,"w")  
    while True:  
        textObj = origFileObj.readline()  
        if textObj[0] == "#":  
            continue  
        elif textObj == "":  
            break  
        else:  
            modFileObj.write(textObj)  
    origFileObj.close()  
    modFileObj.close()  
Run Code Online (Sandbox Code Playgroud)

这给了我这个错误:

Traceback (most recent call last):
  File "<pyshell#96>", line 1, in <module>
    copyAndWriteExcludingPoundSigns_Bad("test.dat","testOutput.dat")
  File "<pyshell#94>", line 6, in copyAndWriteExcludingPoundSigns_Bad
    if textObj[0] == "#":
IndexError: string index out of range
Run Code Online (Sandbox Code Playgroud)

tro*_*tor 5

如果你这样做

if textObj[0] == "#":
和textObj =""然后在零索引处没有字符,因为字符串是空的,因此索引错误.

另一种方法是做

 if textObj.startswith("#"):
这将在两种情况下都有效.


Pau*_*ine 5

一些提示(请阅读PEP8):

  • 使用'for'而不是'while'循环
  • python 2.4之后无需使用readlines
  • 在测试第一个char之前测试该行是否为空

未经测试:

def copy_and_write_excluding_pound_signs(original, filtered):
    original_file = open(original,"r")
    filtered_file = open(filtered,"w")
    for line in original_file:
        if line and line[0] == '#':
            continue
        filtered_file.write(line)
    original_file.close()
    filtered_file.close()
Run Code Online (Sandbox Code Playgroud)

您可能还想过滤一条带有'#'空格的行:

import re

def copy_and_write_excluding_pound_signs(original, filtered):
    pound_re = re.compile(r'^\s*#')
    original_file = open(original,"r")
    filtered_file = open(filtered,"w")
    for line in original_file:
        if pound_re.match(line):
            continue
        filtered_file.write(line)
    original_file.close()
    filtered_file.close()
Run Code Online (Sandbox Code Playgroud)