Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reading a file

Reply
Thread Tools

Reading a file

 
 
zaheer.agadi@gmail.com
Guest
Posts: n/a
 
      02-14-2009
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
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      02-14-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) 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..?


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.

 
Reply With Quote
 
 
 
 
Philipp Pagel
Guest
Posts: n/a
 
      02-14-2009
(E-Mail Removed) 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.

cu
Philipp

--
Dr. Philipp Pagel
Lehrstuhl f. Genomorientierte Bioinformatik
Technische Universität München
http://mips.gsf.de/staff/pagel
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-15-2009
Philipp Pagel wrote:

> (E-Mail Removed) 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:


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.


--
Steven

 
Reply With Quote
 
zaheer.agadi@gmail.com
Guest
Posts: n/a
 
      02-15-2009
On Feb 15, 10:27*am, Steven D'Aprano <(E-Mail Removed)> wrote:
> Philipp Pagel wrote:
> > (E-Mail Removed) 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:

>
> 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.
>
> --
> Steven


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

-Zaheer
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      03-05-2009
In article <(E-Mail Removed)>,
Terry Reedy <(E-Mail Removed)> wrote:
>
>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=')
--
Aahz ((E-Mail Removed)) <*> http://www.pythoncraft.com/

"All problems in computer science can be solved by another level of
indirection." --Butler Lampson
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      03-05-2009
Aahz wrote:
> In article <(E-Mail Removed)>,
> Terry Reedy <(E-Mail Removed)> wrote:
>> 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=')

Or:

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

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      03-05-2009
On Mar 6, 1:28*am, MRAB <(E-Mail Removed)> wrote:
> Aahz wrote:
> > In article <(E-Mail Removed)>,
> > Terry Reedy *<(E-Mail Removed)> wrote:
> >> 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=')

>
> 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 '='
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading of file by next of map file and by next of file descriptor. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 1 07-10-2007 02:46 AM
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muñoz ASP .Net 3 06-04-2005 10:56 AM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments