检查,以确保的Python:检查制表符分隔的文件中列的适当数量
一)每行4列长
二)确保它不会失败,如果那里有一个新的('\ 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
它会给你什么错误吗?你会得到什么?你想检查'如果lineLength <2和我
Moj
2013-05-03 18:09:16
我想你应该检查'lineLength!= 4' – Moj 2013-05-03 18:13:36
我们能得到约5行样本数据作为预期的输入? – Droogans 2013-05-03 18:16:00