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

Ben Ganzfried ben.ganzfried at gmail.com
Tue Dec 21 17:07:54 CET 2010


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)


More information about the Tutor mailing list