久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Python 中對文本文件中的數字求和

How to sum numbers from a text file in Python(如何在 Python 中對文本文件中的數字求和)
本文介紹了如何在 Python 中對文本文件中的數字求和的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個代碼依賴于我讀取文本文件,打印出有數字的數字,打印出有字符串而不是數字的特定錯誤消息,然后將所有數字相加并打印它們的總和(然后僅將數字保存到新的文本文件中).

I have a code that relies on me reading a text file, printing off the numbers where there are numbers, printing off specific error messages where there are strings instead of numbers, then summing ALL the numbers up and printing their sum (then saving ONLY the numbers to a new text file).

我已經嘗試這個問題幾個小時了,我有下面寫的內容.

I have been attempting this problem for hours, and I have what is written below.

我不知道為什么我的代碼似乎沒有正確總結.

I do not know why my code does not seem to be summing up properly.

還有python代碼:

And the python code:

f=open("C:\Users\Emily\Documents\not_just_numbers.txt", "r")
s=f.readlines()
p=str(s)

for line in s:
    printnum=0
    try:
        printnum+=float(line)
        print("Adding:", printnum)    
    except ValueError:
        print("Invalid Literal for Int() With Base 10:", ValueError)

    for line in s: 
        if p.isdigit():
        total=0            
            for number in s:    
                total+=int(number)
                print("The sum is:", total)

推薦答案

我有一個代碼,它依賴于我讀取文本文件,打印出有數字的數字,打印出特定的錯誤消息其中有字符串而不是數字,然后將所有數字起來并打印它們的總和(然后只將數字保存到新的文本文件).

I have a code that relies on me reading a text file, printing off the numbers where there are numbers, printing off specific error messages where there are strings instead of numbers, then summing ALL the numbers up and printing their sum (then saving ONLY the numbers to a new text file).

所以你必須做到以下幾點:

So you have to do the following:

  1. 打印數字
  2. 當沒有數字時打印一條消息
  3. 將數字相加并打印出總和
  4. 僅將數字保存到新文件中

這是一種方法:

total = 0

with open('input.txt', 'r') as inp, open('output.txt', 'w') as outp:
   for line in inp:
       try:
           num = float(line)
           total += num
           outp.write(line)
       except ValueError:
           print('{} is not a number!'.format(line))

print('Total of all numbers: {}'.format(total))

這篇關于如何在 Python 中對文本文件中的數字求和的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Sum values in a dict of lists(列表字典中的總和值)
What is a subtraction function that is similar to sum() for subtracting items in list?(什么是類似于 sum() 的減法函數,用于減去列表中的項目?)
Python equivalent of sum() using xor()(Python 等價于 sum() 使用 xor())
sum matrix columns in python(python中的求和矩陣列)
sum of N lists element-wise python(N個列表元素的總和python)
Python sum() function with list parameter(帶有列表參數的 Python sum() 函數)
主站蜘蛛池模板: 国产精品久久久久久久免费观看 | 亚洲 中文 欧美 日韩 在线观看 | 国产精品一区二区电影 | 欧美不卡视频 | 精品一区二区久久久久久久网精 | 91久久精品国产91久久 | 97超碰在线免费 | 国产精品久久久久久 | av网站在线播放 | 91视频入口| 久久国产免费看 | 久久精品国产免费一区二区三区 | 中文字幕日韩一区二区 | 国产在线一区观看 | 午夜视频在线观看网址 | 中文字幕av在线播放 | 天堂网avav | 日韩在线欧美 | 天天操天天射综合网 | 成人免费在线视频 | 国产精品久久久亚洲 | 欧美一级欧美一级在线播放 | 国产精品久久久久一区二区三区 | 国精日本亚洲欧州国产中文久久 | 日本三级日产三级国产三级 | 成人深夜福利网站 | 国产午夜在线 | 特级做a爰片毛片免费看108 | 欧美日韩国产一区二区三区 | 亚洲国产成人久久久 | 欧美三区在线观看 | 国产精品一区二 | 亚洲国产成人精品女人久久久 | 亚洲视频一区在线播放 | 欧美日韩国产传媒 | 精品欧美一区二区三区久久久 | 国产精品爱久久久久久久 | 欧美激情在线一区二区三区 | 色综合久 | 九九热这里只有精品6 | 毛片a|