Reading sub-string from a file

Chris Rebert clp2 at rebertia.com
Thu Feb 16 11:24:49 CET 2012


On Thu, Feb 16, 2012 at 1:52 AM, Smiley 4321 <ssmile03 at gmail.com> wrote:
> All,
>
> I am a python newbie.
>
> Let's say I have a filename (test.conf) as below -
>
> ----
> int Apple(int, int);
> void Jump_OnUnload(float, int);
> int Jockey_Apple_cat_1KK(float, int, char, int);
> int Jockey_Apple_cat_look(int, float, int, int);
> int Jockey_Apple_cat_test_ki21es(int, int, int, int);
> int Jockey_Apple_cat_tarLK12OU(void, int, int, int);
> ---
>
> Here substring "Jockey_Apple_cat" is common from 3rd line onwards as a
> function name. I wish to extract ONLY that function name which has
> 'Jockey_Apple_cat' as a substring and ignore remaining function names.
>
> The program .py written is -
>
> -----
> ---
> #!/usr/bin/python
>
> def foo(filename):
>         try:
>                 one = open(filename, 'r')
>         except:
>                 print filename, 'cannot be open.'
>
>         AllLines = one.readline()
>
>         for eachLine in AllLines:
>                 start = eachLine.find('Jockey_Apple_cat')
>                 if start != -1:
>                         finish = eachLine.find ('(')
>                         print eachLine[start:finish]
>
>         handle.close()
>
> set_foo("test.conf")
> -----
>
> I did perform debugging using pdb, it only reads all the lines.
>
> Plz help!!

Several oddities in the code you posted (e.g. you defined foo() but
instead call set_foo(); the `handle` variable comes out of nowhere)
suggest that it isn't your actual code; this greatly hinders us in
assisting you. Please post your /actual/ code verbatim (or as much of
it as you can with as few modifications as you can).
Also, avoid using "plz" in the future; it's gotten a reputation for
being associated with undesirable folk.

Cheers,
Chris



More information about the Python-list mailing list