Python:如何对一行完整的整数逐个求和?

Python:如何对一行完整的整数逐个求和?,python,python-3.x,integer,sum,line,Python,Python 3.x,Integer,Sum,Line,我有几行整数(来自一个.txt文件),我想对每行的每个单整数逐一求和 例如: 37107287533902102798797998220837590246510135740250 line0 = sum_of_numbers 46376937677490009712648124896970078050417018260538 line1 = sum_of_numbers 74324986199524741059474233309513058123726617309629 . 9194221336

我有几行整数(来自一个.txt文件),我想对每行的每个单整数逐一求和

例如:

37107287533902102798797998220837590246510135740250 line0 = sum_of_numbers
46376937677490009712648124896970078050417018260538 line1 = sum_of_numbers
74324986199524741059474233309513058123726617309629 .
91942213363574161572522430563301811072406154908250 .
23067588207539346171171980310421047513778063246676 .
89261670696623633820136378418383684178734361726757 .
28112879812849979408065481931592621691275889832738 .
44274228917432520321923589422876796487670272189318 .
47451445736001306439091167216856844588711603153276 .
70386486105843025439939619828917593665686757934951 line9 = sum_of_numbers 

您可以使用类似这样的方法来迭代文件的行并求和这些数字

with open('file.txt', 'r') as f:
    for line in f:
        print(sum(int(char) for char in line.strip()))
如果有可能您的行可能不是所有数字,您将需要使用它来检查

print(sum(int(char) for char in line if char.isdigit()))
# no need for .strip() since \n characters are not a digit

分享你迄今为止所做的尝试,我们可以在正确的方向上帮助你。这看起来像是一项任务question@steven-萨默斯:在使用
.isdigit()
时,您可以省略
.strip()
,然后我将其添加到checkTrue中
print(sum(int(char) for char in line if char.isdigit()))
# no need for .strip() since \n characters are not a digit