Python Forum
read and extract entries in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read and extract entries in python
#1
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()
Reply
#2
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:]**
Reply
#3
(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.
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Ldap3 Python print(conn.entries) doesnt work ilknurg 15 5,767 Dec-28-2022, 11:22 AM
Last Post: shad
  python extract mg24 1 952 Nov-02-2022, 06:30 PM
Last Post: Larz60+
  Python Graphics Help With Entries BadenJaden 1 51,844 Jul-24-2019, 10:36 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020