Python Forum
read a particular part from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read a particular part from a file
#1
Hello community.

I want to search in a text file for a Keyword to readout what is in the same line after the keyword.
Keyword and the contend i look for are seperated with a vertical dash "|"
Like this:

Source:

wordone|blaone
wordtwo|blatwo
wordthree|blathree
wordfour|blafour
...

Keyword is "wordfour"

read out result:
blafour

currently I have this code:

fo = open(filename, "rt")
#print ("Name of the file:\n", fo.name, '\n')

line = fo.readlines()
print ("Read Line: \n %s" %(line))
_____________________________________________________________
thanks for answering
Reply
#2
You need to loop through the lines with a for loop, split them by '|', and check the first part with an if statement.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Just to show what @ichabod801 talk about.
When you do fo.readlines() it put contend in a list,which is not needed for this task.
The is the basic way to loop through the lines.
with open('lines.txt') as f:
    for line in f:
        print(line.strip())
Output:
wordone|blaone wordtwo|blatwo wordthree|blathree wordfour|blafour
This will solve it,i know it look like homework and we do not do that.
But %s the oldest Python string formatting is now irritating me,even it maybe should not Dodgy
I have use f-string in all i have done since 3.6 came out.
keyword = "wordfour"
with open('lines.txt') as f:
    for line in f:
        if keyword in line:
            print(f'Line with found keyword:\n{line.strip()}')
            print('-' * 10)
            print(f"Found keyword <{line.split('|')[1].strip()}>")
Output:
Line with found keyword: wordfour|blafour ---------- Found keyword <blafour>
Reply
#4
Thank you. That helps a lot.
My next trial is to to extract my informations out of a line.

Like this:
+0125F|6t|INFO|jd|uiek|kd_xy

intendet result:
INFO

The "keyword" in this case is the "|" after INFO

rigt now i have this code:
keyword = '+025C|-1|NA|NA|'
with open('Q2P.txt') as f:
    for line in f:
        if keyword in line:
            #print(f'Line with found keyword:\n{line.strip()}')
            print('-' * 25)
            print(f"Stage \n{line.split('_')[1].strip('')}")
            break
I think the key to my problem is in ".strip('')" But i didn't find it yet.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Recommended way to read/create PDF file? Winfried 3 2,864 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,421 Nov-09-2023, 10:56 AM
Last Post: mg24
  error "cannot identify image file" part way through running hatflyer 0 662 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  read file txt on my pc to telegram bot api Tupa 0 1,096 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,102 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,248 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,464 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,191 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,583 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 1,913 Jan-25-2023, 04:12 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

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