最后一个是close,您正在为每行中的每个数字创建一个浮点列表;唯一的问题是你只使用了第一个。您需要在列表上循环:
with open(os.path.join(path,"config.txt"),"r") as config:
for line in config:
numbers_float = map(float, line.split())
for number in numbers_float:
print number
你也可以把事情弄平
with open(os.path.join(path,"config.txt"),"r") as config:
splitlines = (line.split() for line in file)
flattened = (numeral for line in splitlines for numeral in line)
numbers = map(float, flattened)
for number in numbers:
print number
现在只是
a chain of iterator transformations
如果需要,您可以使该链更加简洁:
with open(os.path.join(path,"config.txt"),"r") as config:
numbers = (float(numeral) for line in file for numeral in line.split())
for number in numbers:
print number
但我认为这实际上并不清楚,尤其是对于新手来说。