read in a list in a file to list

Rick Johnson rantingrickjohnson at gmail.com
Sat Apr 8 22:56:12 EDT 2017


@John

General debugging methodology dictates that when your output
does not match your expectation, you must never assume
anything. Here you made the fatal mistake of assuming that:
(1) files are stored as list objects, or (2) Python
automatically converts file data to list objects, or (3)
that python can read your mind, and knowing that you wanted
a list, gave you a list. But in any of those cases, you
would be wrong.

To discover the source of the problem, use Python's
wonderful introspection capabilities. In this case, the
built-in function named "type" is your friend.

> apefile = open("apefile.txt")
> apelist = apefile.read()

print(type(apelist))

I would also make a slight quibble reguarding your choice of
variable names. I would recommend "fileObj" and "fileData"
respectively. Unless you are comparing the contents of two
or more files (say: "apeData", "monkeyData" and
"marmosetData") there is no need for the "ape" mnemonic. Use
a generic name instead.


More information about the Python-list mailing list