0

我有一个具有以下结构的文件:标题 - 数据 - 空白行 - 标题......我正在尝试在标题和第一个空白行之间打印我的数据文件的行。我正在使用的那段代码似乎不起作用;你有什么建议吗?

for j in range(0, number_of_angles, 1):
    start_line = omega_line_number[j]+5 #start line is the line number after the header
    print start_line
    for line in range(start_line,num_lines,1): #num_lines is the total number of lines
        stripped = line.strip()
    if not stripped:
        break
    print line
4

2 回答 2

0

line将是一个integer,并且没有方法,strip()

于 2013-10-22T20:21:15.480 回答
0

我写了一个快速程序,可以处理我为此制作的文本文件。基本上,文本文件包含 10 行包含单词“header”和 20 行包含单词“body”。之后,我有 10 个空白行并重复前 30 行。这是仅打印正文的代码。

if __name__ == '__main__':                                                       
    # Open the file for reading.                                                 
    rd_file_name = "../txt/q1.txt"                                               
    rd_file = open(rd_file_name, 'r')                                            

    # Read through the header                                                    
    for line in rd_file.readlines():                                             
        # Decide what to do based on the content in the line.                    
        if "header" in line.lower():                                             
            # Don't print the header                                             
            pass                                                                 
        elif line.strip() == "":                                                 
            # Quit if you see a blank line                                       
            break                                                                
        else:                                                                    
            # We print the body.  Lines end with a carriage return, so we don't  
            # add another one.                                                   
            print line,                                                          

    # Clean up                                                                   
    rd_file.close() 
于 2013-10-22T20:30:42.577 回答