简体   繁体   中英

Reading Formatted files in python

I have a text file that looks like this:

Time: 2014030218

Add: India
Name: Sourav

 k io res tec e
 1 4  3   9   2
 2 3  1   4   4

Time: 2014030300

Add: India
Name: Sourav

 k io res tec e
 1 3  4   8   3
 2 2  2   6   4
 3 2  3   6   6

I want to have a new file similar to this but skipping the Add and Name information from each time step (as its common for every time step). Furthermore, I want to have only those rows which satisfy an restriction on the res and tec columns, something like if 1<=res<=3 and 4<=tec<=7 .

So it should look like this.

Time: 2014030218

 k io res tec e
 1 3  1   4   4

Time: 2014030300

 k io res tec e
 1 2  2   6   4
 2 2  3   6   6

Note: k is serial number.

Read each line of the file into a variable line and use tests for line.startswith('Add: ') and line.startswith('Name: ') and skip those lines while writing out the others. Keep track of whether you are in the lines following the k io ... line to check on the actual values (and skip again if appropriate:

with open('input.txt') as ifp:
    with open('output.txt', 'w') as ofp:
        tec_seen = False
        empty_line = False
        for line in ifp:
            start_word = line.split(':', 1)[0]
            if start_word in ('Add', 'Name'):
                continue
            if start_word == 'Time':
                tec_seen = False
            if line.lstrip().startswith('k io res tec e'):
                tec_seen = True
                ofp.write(line)
                continue
            if tec_seen and line.strip():
                vals = line.split()
                res = int(vals[2])
                tec = int(vals[3])
                if not 1 <= res <= 3:
                    continue
                if not 4 <= tec <= 7:
                    continue
            else:
                if not line.strip():
                    if empty_line:
                        continue
                    empty_line = True
                else:
                    empty_line = False
            ofp.write(line)

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