简体   繁体   中英

Readlines method in Python skipping the first line in a file after heading

filename = os.path.abspath(r'C:\x\y\Any.ini') #Using absolute path
file = (open(filename, 'r', encoding='UTF-8'))
for line in file:
    if ("Heading A") in line:
        for line in file:
            out = file.readlines()[1:]
                    print(out)

Inside File the structure is

[Heading A] #I don't want to read 
a[0]    # Not being read although i want to
a[1]    # Starts to be read in the program
b 
c

I also tried with

file.read().splitlines()

Now I am getting prints from a[1]. a[0] is always being skipped.

Is there anything i am missing out to continue reading from 2nd line of the file

try this:

firstLine = file.readline()
    if firstLine.startsWith("[Heading A]"):
        for line in file:
            //code

You have a sane configuration file. Read the following;

https://docs.python.org/3/library/configparser.html

The following might work.

filename = os.path.abspath(r'C:\x\y\Any.ini') #Using absolute path
file_lines = open(filename, 'r', encoding='UTF-8').readlines()[1:]
for line in file_lines:
    print(line)

To Add some explanations:

For reading a file line by line, see here .

Your problem is that you are using multiple calls that each read one or multiple lines from the file and that line is gone for the next reading call - see my comments in the code:

for line in file: // reads the first line and would read again if we came back here before the end of the file, which we do not
if ("Heading A") in line:
    for line in file: // reads the second line of the file and would read again if we came back here before the end of the file, which we do not
        out = file.readlines()[1:] // reads all remaining lines from the file ( beginning from the third) and drops the first (line three in the file) by indexing [1:]
                print(out) // prints out all lines begining with the fourth; after this, the file is at its and and both for loops will be finished

What you want to do is reading line by line and dropping the on containing Heading A :

filename = os.path.abspath(r'C:\x\y\Any.ini') #Using absolute path
file = (open(filename, 'r', encoding='UTF-8'))
for line in file:
    if not ("Heading A") in line: print (line)

This is because the read pointer (or the stream position to be specific) advances as you iterate through the file. In your case, the two for loops are to be held responsible for it. When you call readlines() in the second loop, it only loops through the remaining lines in the file, and hence it looks like it is skipping lines.

Since you want to read lines post 'Heading A', you can simply read all lines once you encounter it. The code for the same should look something like:

filename = os.path.abspath(r'C:\x\y\Any.ini')
file = (open(filename, 'r', encoding='UTF-8'))
for line in file:
    if ("Heading A") in line:
        out = file.readlines()[
        print(out)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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