1. 程式人生 > >python提取字串中的數字

python提取字串中的數字

字串儲存在string.txt中,將字串中的數字提取出來,組成心得字串,並列印輸出。

#!/usr/bin/env python3
file=open('/home/user/string.txt')
file_context=file.read()
i=0
string=''
while i < len(file_context):
    if file_context[i].isdigit():
        string+=file_context[i]
    i+=1
 print(string)

或者

file=open('test.txt','r')
try:
    for
line in file: print line finalli: file.close()