with open('data.txt') as f: polyShape = [] for line in f: line = line.split() # to deal with blank if line: # lines (ie skip them) line = [int(i) for i in line] polyShape.append(line)会给你
[[0, 0, 3, 50], [50, 100, 4, 20]]
这将适用于包含(或不包含)空行的文件。
with完成操作或遇到异常时,使用该构造将自动为您关闭文件。



