[Tutor] Callable? Whats callable?

Sheila King sheila@thinkspot.net
Sun, 26 Aug 2001 09:54:07 -0700


On Sun, 26 Aug 2001 12:13:16 -0400, "epoch7" <epoch7@charter.net>  wrote
about Re: [Tutor] Callable? Whats callable?:

OK, you have a few problems here, which mostly have to do with mixing
together "types" that don't go together.

For example, if you have an open file, then you can do:

file1.read()   ... if you opened it for reading
or
file2.write()  ... if you opened it for writing.

But, if x is a string, then you can't do
x.write()
or
x.read()

Because .read() and .write() are file methods. They don't work on
strings, or on regular expression patterns and such. (Well, when you get
a bit more advanced, there are things that aren't *strictly* files, that
have file-like methods, but let's not get into that today.)

:alright. so i fixed the error in y. originally i started putting in the
:read()s and stuff to be able to fix an error/find a workaround.
:i was getting, here's how i got that error:
:
:import re
:
:file1 = raw_input("Name of file to convert: ")
:file2 = raw_input("Name of file to output to: ")
:in_file = open(file1, "r")
:out_file = open(file2, "w")
:x = in_file.read()
:text = re.compile('url=(\w*)&', re.IGNORECASE)
:y = text.findall(x)
:y.write(text)
:in_file.close()
:out_file.close()
:
:and here's the error
:
:Traceback (most recent call last):
:  File "F:\Python21\reusage.py", line 11, in ?
:    y.write(text)
:AttributeError: write

At first I was going to suggest that you change the line

y.write(text)

to out_file.write(text)

But, if you do, you will get this error:

Traceback (most recent call last):
  File "C:\My Documents\temp.py", line 12, in ?
    out_file.write(text)
TypeError: argument must be string or read-only character buffer, not
SRE_Pattern

The point is, y.write(text) isn't going to work, because y isn't a file,
and .write() is a file method.

Now, if you try

out_file.write(text)
at least out_file is a file. So this is a bit better. HOWEVER, text
isn't a string. And when you try to write stuff to a file, it has to be
a string.

I looked up the findall() command (since I'm not familiar with it) and
it says that findall() returns a list of all non-overlapping matches of
pattern in string. Well, there's a problem. text is a list, not a
string.

I'm not sure how to advise you on this, exactly, since I don't know the
regular expression stuff. 

You write that you are trying to do this:
:And for those of you just wondering what im trying to do, its supposed to search out for strings of text
:between to strings("url=" and "&").  and then output the list of strings to a file.

It is possible to do this with just string operations. If you'd like to
try that, see:
http://www.python.org/doc/current/lib/string-methods.html


I hope some of this explanation helped.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/