将它们读入列表很简单readlines():
f = open('your-file.dat')
yourList = f.readlines()
Run Code Online (Sandbox Code Playgroud)
如果你需要删除新行,你可以使用ars'方法,或者:
yourList = [line.rstrip('\n') for line in f]
Run Code Online (Sandbox Code Playgroud)
如果你想要一个从1到列表长度的字典的字典,首先想到的方法是按上面的方式制作列表,然后执行:
yourDict = dict(zip(xrange(1, len(yourList)+1), yourList))
Run Code Online (Sandbox Code Playgroud)