所以我寫這個的Python程序,必須從包含衆多坪的文本文件,什麼在文本文件中我下面預覽提取往返時間:解析浮在文本文件中的ping輸出數量
64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=1 ttl=60 time=12.6ms
64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=2 ttl=60 time=1864ms
64 bytes from a104-100-153-112.deploy.static.akamaitechnologies.com (104.100.153.112): icmp_seq=3 ttl=60 time=107.8ms
我想從文本文件中提取的是12.6,1864和107.8。我用正則表達式來做到這一點,有以下幾點:
import re
ping = open("pingoutput.txt")
rawping = ping.read()
roundtriptimes = re.findall(r'times=(\d+.\d+)', rawping)
roundtriptimes.sort()
print (roundtriptimes)
我遇到的問題是,我相信數字正在讀入roundtriptimes列表作爲字符串,所以當我去整理他們,他們不排序。因爲我希望他們。
任何想法如何修改我的正則表達式findall命令,以確保它將它們識別爲數字將極大地幫助!謝謝!
交叉貼:http://unix.stackexchange.com/questions/314093/parsing-text-file-in-python/314095#314095http://unix.stackexchange.com/questions/314093/解析文本文件的蟒蛇/ 314095#314095我已經回答了你,即使**完整工作** python代碼... –
@GillesQuenot我剛剛發佈了答案後看到了這一點。這非常不幸。事實上,你的答案中有一個功能完整的Python解決方案(實際上是一個比我的更好的正則表達式)。 – idjaw