Edit: Sorry for the confusion everyone. The first version of this question is so confusing I that have decided to delete it.
Here is my question again.
How do I translate a list of 10,000 student names开发者_高级运维 (text file) to a list that can be iterated through?
I initially said dictionary, but I actually meant list, I apologize.
Assuming that each student's name is on its own line, which is what it sounds like, this is as easy as
students = open('list-of-students.txt').readlines()
which will make students
be a list
of all the students in the file.
It depends strongly on what you want to do.
But something like this should work:
with file("filename-with-students.txt") as f:
for line in f:
## now, the name "line" refers to a string containing a line in your file
The first line, with file(...) as f
, opens the file and assigns the "file descriptor" the name f
. Using a with
statement means that it will automatically be closed when you're done.
The second line, for line in f
, takes advantage that you can iterate over files, one line at a time. Hence, you can do whatever you want the string that line
refers to.
精彩评论