Reading a file

Discussion in 'Python' started by zaheer.agadi@gmail.com, Feb 14, 2009.

  1. Guest

    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
     
    , Feb 14, 2009
    #1
    1. Advertising

  2. Terry Reedy Guest

    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.
     
    Terry Reedy, Feb 14, 2009
    #2
    1. Advertising

  3. 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
     
    Philipp Pagel, Feb 14, 2009
    #3
  4. Philipp Pagel wrote:

    > 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
     
    Steven D'Aprano, Feb 15, 2009
    #4
  5. Guest

    On Feb 15, 10:27 am, Steven D'Aprano <> wrote:
    > Philipp Pagel wrote:
    > > 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
     
    , Feb 15, 2009
    #5
  6. Aahz Guest

    In article <>,
    Terry Reedy <> 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 () <*> http://www.pythoncraft.com/

    "All problems in computer science can be solved by another level of
    indirection." --Butler Lampson
     
    Aahz, Mar 5, 2009
    #6
  7. MRAB Guest

    Aahz wrote:
    > In article <>,
    > Terry Reedy <> 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=')):
     
    MRAB, Mar 5, 2009
    #7
  8. John Machin Guest

    On Mar 6, 1:28 am, MRAB <> wrote:
    > Aahz wrote:
    > > In article <>,
    > > Terry Reedy  <> 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 '='
     
    John Machin, Mar 5, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    699
    Kevin Spencer
    Nov 11, 2004
  2. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,443
    Scott Allen
    Jun 4, 2005
  3. Replies:
    0
    Views:
    807
  4. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    393
    Hrvoje Niksic
    May 25, 2007
  5. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    823
    Alf P. Steinbach
    Jul 10, 2007
Loading...

Share This Page