beginner's questions - manipulating text files
Cédric Lucantis
omer at no-log.org
Wed Jul 2 06:43:36 EDT 2008
Le Wednesday 02 July 2008 01:16:30 Ben Keshet, vous avez écrit :
> Hi,
>
> I am a very beginner Python programmer with limited programming
> experience overall.
>
> I am trying to write a script that will search for the second and third
> appearance of the symbol '@' in a file, will read a random line between
> them, and write the line into a new file. So far I was only able to open
> the file using 'open', but I am not sure how to proceed. I tried to read
> it line by line using 'readline' but was not sure how to manipulate the
> text the way I need to.
>
If the file you're reading is not too big, you can use file.readlines() which
read all the files and returns its content as a list of lines.
> Could anyone please give me |a basic guidance as for what functions may
> be useful for my purpose? (e.g how to search for a string '@' in a text?
> how to identify its location?
text.find('@') will return the position of the first occurence of '@', or a
negative value if not found.
> how to choose a random number in a defined
> range?
random.randrange(start, stop)
> how to read that line number from a text file? etc.)|
if you read the file with readlines(), just lines[lineno]
you'll find more infos in the following sections:
http://docs.python.org/lib/bltin-file-objects.html
http://docs.python.org/lib/string-methods.html
http://docs.python.org/lib/module-random.html
--
Cédric Lucantis
More information about the Python-list
mailing list