Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20509 > unrolled thread

Re: Reading sub-string from a file

Started byPeter Otten <__peter__@web.de>
First post2012-02-16 11:25 +0100
Last post2012-02-16 11:25 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Reading sub-string from a file Peter Otten <__peter__@web.de> - 2012-02-16 11:25 +0100

#20509 — Re: Reading sub-string from a file

FromPeter Otten <__peter__@web.de>
Date2012-02-16 11:25 +0100
SubjectRe: Reading sub-string from a file
Message-ID<mailman.5883.1329387966.27778.python-list@python.org>
Smiley 4321 wrote:

> I am a python newbie.

Welcome!
 
> 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.

There are errors in the above that suggest that you retyped your actual 
code. Don't do that! Always use cut and paste for code snippets you are 
posting here.

As to what the problem in your actual code might be, here's a hint:
You could be iterating over the characters of the first line of the file 
instead of the lines. Change

>         AllLines = one.readline()
> 
>         for eachLine in AllLines:

to 

for eachLine in one:

to fix that.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web