久久久久久久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() 函數)
主站蜘蛛池模板: 国产综合久久久 | 国产色视频一区二区三区qq号 | av片在线免费观看 | 欧美日韩在线一区二区三区 | 日韩欧美二区 | 亚洲第一在线 | 91精选视频 | 午夜在线影院 | 看国产毛片 | 亚洲精品成人 | 午夜天堂av| 天天插天天狠天天透 | 波多野结衣视频一区 | 久草免费福利视频 | 精品www | 国产精品网站在线观看 | 中文字幕一区二区三区在线观看 | 国产免费av在线 | 亚洲av毛片成人精品 | 九九热这里只有 | 国产一区在线视频 | 四虎影院永久免费 | 国产精品第一区 | 亚洲精品18在线观看 | 日韩在线精品视频 | 视频一二三区 | 日本高清视频www | 中文字幕精品三区 | 亚洲精品少妇 | 国产精品久久久久久中文字 | 91久久久久久久 | 免费一级全黄少妇性色生活片 | 手机av免费| a在线视频 | 国产超碰在线 | 欧美久久精品 | 岛国av在线播放 | 天天色影院 | 日本特黄一级片 | 99一区二区三区 | 五月天精品 |