473,396 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Re: beginner's questions - manipulating text files

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
1 1574
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Atip Asvanund | last post by:
Dear sirs, I am trying to learn how to use Boehm's garbage collector: http://www.hpl.hp.com/personal/Hans_Boehm/gc/ on a Linux machine. I am a beginner, and I find its documentation inadequate....
4
by: Pekka Karjalainen | last post by:
Hi, this is my first posting here. I also have a question about etiquette. There's a html file associated with my question. I cannot host it indefinitely at the current location. I don't, however,...
27
by: MHoffman | last post by:
I am just learning to program, and hoping someone can help me with the following: for a simple calculator, a string is entered into a text box ... how do I prevent the user from entering a text...
23
by: Rick | last post by:
Hi, I'm currently preparing for an interview on VB.Net Development.. could someone please give me an idea as to what type of questions can one ask (it's a practical test) and what sort of things...
2
by: Ido Flatow | last post by:
Hi all, I've been exploring the way I can manipulate WSDL.exe using SchemaImporterExtension in order to create a proxy to my liking. My situation is as follows - I have a web site that has...
14
by: shamirza | last post by:
Question Do ActiveX DLLs made in VB still need the VB runtimes on the machine? ________________________________________ Answer In a word, Yes. Visual Basic does not support what is known...
10
by: See_Red_Run | last post by:
Hi, I am trying to figure out how to get started with PHP/MySQL. Everything I've read so far says to start with PHP first. I was expecting something like Visual Basic Express or some other type...
2
by: Daniel | last post by:
Looking for a good resource for beginners? books, online anything that will get me started.. what im trying to focus on is building a small program in c# to look thru a few files in a folder...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.