Reading a file

Z

zaheer.agadi

Hi

How do i read a file in Python and search a particular pattern
like I have a file char.txt which has

Mango=sweet
Sky=blue

I want to get the strings sweet and blue,How to do this..?

Thanks
 
T

Terry Reedy

Hi

How do i read a file in Python and search a particular pattern
like I have a file char.txt which has

Mango=sweet
Sky=blue

I want to get the strings sweet and blue,How to do this..?

for line in open('char.txt'):
if line.find('sweet') != -1 or line.find('blue') != -1:
print(line)

Read the section of the Lib Manual on string methods.
 
P

Philipp Pagel

How do i read a file in Python and search a particular pattern
like I have a file char.txt which has

I want to get the strings sweet and blue,How to do this..?

If your entire file consists of such key=value pairs you may want to properly parse them:

for lne in infile:
line = line.rstrip()
key, value = line.split('=')
if key in ('Mango', 'Sky'):
print value

Or something like that - details depend on what exactly your criteria for
picking the values are, of course.

cu
Philipp
 
S

Steven D'Aprano

Philipp said:
If your entire file consists of such key=value pairs you may want to
properly parse them:

The proper way to parse them would be with the ConfigParser module.

The only negative with ConfigParser is that it requires a section label. In
my experience it is quite common for INI files with only a single section
to leave out the label.
 
Z

zaheer.agadi

The proper way to parse them would be with the ConfigParser module.

The only negative with ConfigParser is that it requires a section label. In
my experience it is quite common for INI files with only a single section
to leave out the label.

Thanks to all of you,I used to ConfigParser worked fine.

-Zaheer
 
A

Aahz

for line in open('char.txt'):
if line.find('sweet') != -1 or line.find('blue') != -1:
print(line)

For any recent Python, this should be:

if 'sweet' in line or 'blue' in line:

Although I think that for the OP's use case, it ought to be:

if line.startswith('sweet=') or line.startswith('blue=')
 
M

MRAB

Aahz said:
For any recent Python, this should be:

if 'sweet' in line or 'blue' in line:

Although I think that for the OP's use case, it ought to be:

if line.startswith('sweet=') or line.startswith('blue=')
Or:

if line.startswith(('sweet=', 'blue=')):
 
J

John Machin

Or:

     if line.startswith(('sweet=', 'blue=')):

C'est magnifique mais ce n'est pas la guerre ... c'est "bassackwards"
** 2:

(1) 'Mango' is the query, 'sweet' is the desired result
(2) The line, after stripping \n, *ends* with 'sweet'

And it's not robust in the face of likely whitespace either side of
the '='
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top