Python Forum

Full Version: read and extract entries in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I have a file with below sample entries.

2282:depth=3 hl=4 l=12096 cons: start_indi
2286:depth=4 hl=4 l=5321 cons: next
2290:depth=5 hl=4 l=4785 cons: next
2294:depth=6 hl=2 l= 1 base: type :01

i want to achieve the following.
I start reading my entries when i get my predefined indicator **start_indi**
I would like to get the output as **[2286:5321]**, but i am getting it as **[2286:5321 cons: next]**

also i want to continue extracting the offsets and lengths as long as they satisfy the condition

prev_offset + prev_length = next_offset

so in my file the next_offset will be at some random point i will read based on that condition.

please help

below is my code:

fo = open("result.txt")
line = fo.readline()
while len(line.strip()) != 0 :
    if "start_indi" in line:
        line = fo.readline()
        offset = line.split(':')[0] 
        length = line.split('=')[3]
        print("["+ offset + ":" + length + "]")
    line = fo.readline()
Last one has different format, so needs code to compensate for that.

import os

os.chdir(os.path.abspath(os.path.dirname(__file__)))

with open('result.txt') as fp:
    find_starter = True
    for line in fp:
        while find_starter:
            if "start_indi" in line:
                find_starter = False
            continue
        # print(f"line: {line}")
        a = line.split(':')[0]
        b = line.split()[2].split('=')[1]
        print(f"**[{a}:{b}]**")
results:
Output:
**[2282:12096]** **[2286:5321]** **[2290:4785]** **[2294:]**
(Jul-06-2020, 08:11 PM)Larz60+ Wrote: [ -> ]Last one has different format, so needs code to compensate for that.

import os

os.chdir(os.path.abspath(os.path.dirname(__file__)))

with open('result.txt') as fp:
    find_starter = True
    for line in fp:
        while find_starter:
            if "start_indi" in line:
                find_starter = False
            continue
        # print(f"line: {line}")
        a = line.split(':')[0]
        b = line.split()[2].split('=')[1]
        print(f"**[{a}:{b}]**")
results:
Output:
**[2282:12096]** **[2286:5321]** **[2290:4785]** **[2294:]**
I think you mis-understand the question, i dont want to take all the lines, i want to take a line that is found after my start_indi, next i want to find all the lines which satisfies prev_offset + prev_length = next offset line till the end of file.
(Jul-06-2020, 09:43 AM)RRR Wrote: [ -> ]I would like to get the output as **[2286:5321]**, but i am getting it as **[2286:5321 cons: next]**

That's because the length variable is set to line.split('=')[3], which means "everything between the third and fourth equals signs". As there are only 3, length gets set to '5321 cons: next\n'. You'll need to do another split on spaces, or do something more complex like a regex.