[Tutor] AttributeError: 'list' object has no attribute 'find'

Luke Paireepinart rabidpoobear at gmail.com
Tue Dec 21 19:04:10 CET 2010


Read() does return a string. I guess the better question would be... Are you using read? 'cause in the example you sent you used readlines() which returns a list of lines.

-----------------------------
Sent from a mobile device with a bad e-mail client.
-----------------------------

On Dec 21, 2010, at 10:07 AM, Ben Ganzfried <ben.ganzfried at gmail.com> wrote:

> Hey,
> 
> I keep getting the error above and unfortunately browsing through
> google and finding similar responses has not been fruitful for me.  My
> code is below and I have marked off the location of the problem in my
> code.  I'm wondering the following:
> 
> 1) Doesn't the read() file object method return the specified
> characters from the file as a string?
> 2) If #1 is correct, then why is my variable "source" being viewed as
> a list as opposed to a string?
> 3) How can I change my variable "source" so that it can use the 'find'
> method?  Or alternatively, is there another method besides the 'find'
> method that would do the same thing that 'find' does that would work
> on my variable 'source' as it currently is?
> 
> Thanks so much,
> 
> Ben
> 
> #recursively find each instance of the tag throughout the whole document
> def findOneTag (tag, source, output):
>    print("tag is ", tag)
>    #print("source is now ", source)
>    print("output is ", output)
>    #base case
>    if source == "":
>        return
>    #recursive case
>    tagIndex = source.find(tag) #(*********THIS IS THE LOCATION OF THE
> PROBLEM**********)
>    print("tagIndex is ", tagIndex)
>    start = source[tagIndex:].find("\t") + 1
>    print("start is ", start)
>    stop = source[tagIndex + start:].find("\t")
>    print("stop is ", stop)
>    if tagIndex !=-1:
>        output.write(tag + "\t" + line[start: stop])
>        print("spliced text is: ", line[start:stop])
>            #recursively call findOneTag(tag, String source[stop:], output)
>        findOneTag(tag, source[stop + 1:], output)
> 
> def main():
>    tag = "species"
>    inname = "skeletontext.txt"
>    outname = "skeletontext1234567.txt"
>    inname1 = open(inname, "r")
>    output = open(outname, "w")
>    source = inname1.readlines()
>    print("source is: ", source)
>    findOneTag(tag, source, output)
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list