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
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,123 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Read TXT file in Pandas and save to Parquet zinho 2 1,265 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 1,596 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,775 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,326 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 4,797 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,875 Nov-09-2023, 10:56 AM
Last Post: mg24
  error "cannot identify image file" part way through running hatflyer 0 1,992 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  read file txt on my pc to telegram bot api Tupa 0 2,626 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 2,252 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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