Re: saving output in a text file (fwd)
Danny Yoo <dyoo <at> hkn.eecs.berkeley.edu>
2006-07-01 17:40:20 GMT
> If we want to create a text file and write some text into it we use the
> following command in Python:
> myfile = open("test.txt","w")<?xml:namespace prefix = o ns =
> "urn:schemas-microsoft-com:office:office" />
> myfile.write("hello world")
> But what if instead of writing the text 'hello world', i want to write
> the output returned by a regular expression pattern, into the text file,
> how would we specify that in the write command.
Just as a disclaimer: I am doing my utmost not to give out The Answer
here. So if it sounds like I'm not being very direct, that's precisely
because you have a good intuition. *grin* I'm trying to poke at the
source of the problem, and not the immediate side effects.
You still haven't tackled my initial question about:
> def double(s):
> """double: string -> string
> Doubles up the input string s. For example, double("abc")
> should return "abcabc".
> return s + s
> Would you be able to write a program that takes "hello world", runs it
> through double(), and writes out "hello worldhello world" to disk?
Can you try this mini-problem first? What you've done doesn't touch on
the question: would you know how to use double() as a part of the write()?
Note that this is just a variation of your original question, replacing
the regex part with the use of a helper function. It seems like this
should be even easier than the problem you're having now.
Here's why I'm asking this: if you're getting stuck at this point too,
then I'll assume that the problem is that you are unfamiliar with using
helper functions and function composition. Then the group as a whole can
help you with that, since that's a fundamental programming skill that
you'll want to develop.
But if you can do the mini-exercise, then the focus shifts to the use of
regular expressions, and things become much easier, since it'll mean that
you're not familiar with the regex API.
Tutor maillist - Tutor <at> python.org