# coding:utf-8 # 每行求和 def addEachLine(line): values = line.split() values = [int(v) for v in values if v.strip()] return sum(values) def main(): result = [] with open("input.txt") as f: buf = f.read() lines = buf.splitlines() lines = [l for l in lines if l.strip()] # 对拆分的每一行进行计算 for l in lines: result.append(addEachLine(l)) # 打印结果 for r in result: print r if __name__ == '__main__': main()
输出
167
186
167
168
343
299
422
416
159
读出每行数据是一个字符串如 str=