检查以确保
a) 每行有 4 列长
b) 确保程序末尾有新行 ('\n') 时不会失败
def ask_for_filename():
filename=raw_input("Please enter file name: ")
return filename
def read_data(filename):
with open(filename) as f:
data = f.readlines()
i = 0
for line in data:
lineContains = line.split('\t')
lineLength = len(lineContains) #calculate elements
i = i+1
if lineLength < 3 and i < len(data):
print "File is invalid format."
f.close()
return data
您能否纠正我有问题的地方,因为这部分代码不起作用。
i = 0
for line in data:
lineContains = line.split('\t')
lineLength = len(lineContains) #calculate elements
i = i+1
if lineLength < 3 and i < len(data):
print "File is invalid format."
示例文件内容:
完整文件:
AUTHOR(S) YEAR TITLE JOURNAL/CONFERENCE
Accot;Zhai 2001 Scale effects in steering law tasks Proc. ACM CHI
Acredolo 1977 Developmental Changes in the Ability to Coordinate Perspectives of a Large-Scale Space Developmental Psychology
Aginsky;Harris;Rensink;Beusmans 1997 Two strategies for learning a route in a driving simulator Journal of Environmental Psychology
不完整的文件(上述代码适用于此类文件):
AUTHOR(S) YEAR TITLE JOURNAL/CONFERENCE
Accot;Zhai 2001 Scale effects in steering law tasks Proc. ACM CHI
Acredolo Developmental Changes in the Ability to Coordinate Perspectives of a Large-Scale Space Developmental Psychology
Aginsky;Harris;Rensink;Beusmans 1997 Two strategies for learning a route in a driving simulator Journal of Environmental Psychology
Agrawala;Beers;Frohlich;Hanrahan;McDowall;Bolas 1997 The two-user responsive workbench: Support for collaboration through individual views of a shared space Proc. ACM SIGGRAPH
Ahmadabadi;Eiji 1996 Cooperation strategy for a group of object lifting robots Proc. of IROS