By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,841 Members | 840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,841 IT Pros & Developers. It's quick & easy.

Re: beginner's questions - manipulating text files

P: n/a
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
Jul 2 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
CĂ©dric Lucantis <om**@no-log.orgwrites:
Le Wednesday 02 July 2008 01:16:30 Ben Keshet, vous avez Ă©critÂ*:
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.

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.
Better is to iterate over the file object, getting a line each time.
This works regardless of the size of the file, because it doesn't
attempt to read the entirety of a large file in at once.
text.find('@') will return the position of the first occurence of
'@', or a negative value if not found.
If one is interested only in *whether* text is contained within a
string (and is uninterested in its position), the 'in' operator
returns a boolean value.

Untried code:

import random

separator = "@"
interesting_lines = []

input_file = open("foo.txt")
seen_separator_count = 0
for line in input_file:
if separator in line:
# Count this separator, but don't keep the line.
seen_separator_count += 1
continue
if seen_separator_count == 2:
# We have seen exactly two lines with separators,
# so we're interested in the current line.
interesting_lines.append(line)
if seen_separator_count >= 3:
# After seeing three lines with separators, stop reading.
break

chosen_line = random.choice(interesting_lines)
output_file = open("bar.txt", 'w')
output_file.write(chosen_line)

--
\ “Smoking cures weight problems. Eventually.” —Steven Wright |
`\ |
_o__) |
Ben Finney
Jul 2 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.