#Python TQC考題902 資料加總
f=open(“read.txt”,”r”,enconding=”utf-8″)
data=f.read()
L=[int(i) for i in data.split()]
print(sum(L))
f.close()

“””
read.txt的內容:
1 2 3 6 3
(加總=15)
“””

#多印一些資料,了解程式運作:
#read.txt內容: 1 4 2 3 (加總10)

#用.readline()也可以

#少encoding=”utf-8″ 也行

“””
TQC txt檔的資料為:
11 22 33 22 33 44 33 44 55 44 55 66 55 66 77
雖然簡單,但還是可能使用
.append(line.split())
做成list中還有list
“””





![Python: 如何判斷字符串內容是否為數字(整數或浮點數)? isinstance( eval( entry.get() ), (float, int) ) ; str.isdigit() #不包括小數點和負號 ; try~ except ValueError~ ; 正則表示法 regular expression ; pattern = ‘^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$’ Python: 如何判斷字符串內容是否為數字(整數或浮點數)? isinstance( eval( entry.get() ), (float, int) ) ; str.isdigit() #不包括小數點和負號 ; try~ except ValueError~ ; 正則表示法 regular expression ; pattern = ‘^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$’](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2023/05/20230512152430_3.png?quality=90&zoom=2&ssl=1&resize=350%2C233)






近期留言