Reading a file

Philipp Pagel pDOTpagel at
Sat Feb 14 17:31:29 EST 2009

zaheer.agadi at wrote:
> 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..?

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.


Dr. Philipp Pagel
Lehrstuhl f. Genomorientierte Bioinformatik
Technische Universität München

More information about the Python-list mailing list