Python Forum
Need help to retrieve a particular string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help to retrieve a particular string
#1
Hi,

I need your help to retrieve/display particular string in a file.

file1:
=====
567812345676-123
p-45yt7u75402

I want to get the record starts with p- record in a file.

Appreciate your help on this.

Thx in advance
Reply
#2
What have you tried?
Reply
#3
Hi,

I tried using the below but always it should be the last record in a file.

with open(r'C:\Users\lokesh\file.txt') as f:
print(f.readlines()[-1].split())

and even the output is showing like as below

output:
=====
['p-45yt7u75402']

however i don't want to get [(paranthesis)and '(single quotes). Please let me know how to achieve the record starting with p-

Thx in advance
Reply
#4
Maybe you can iterate over all lines of file and check whether line starts with 'p-', something along those lines:

with open(r'C:\Users\lokesh\file.txt') as f:
    for line in f:
        if line.startswith('p-'):
            # do_something
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Retrieve word from string knob 4 481 Jan-22-2024, 06:40 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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