Python 如何从这行中每隔10秒提取百分比?
可能重复:Python 如何从这行中每隔10秒提取百分比?,python,parsing,Python,Parsing,可能重复: 我有一个linux命令: rsync -avz --info=progress2 source:/file /destination 我在Python中使用的方法如下: proc = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', source12, destination], stderr=
我有一个linux命令:
rsync -avz --info=progress2 source:/file /destination
我在Python中使用的方法如下:
proc = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', source12, destination],
stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
receiving incremental file list
rathi/
rathi/20090209.02s1.1_sequence.txt.gz
209,398,969 27% 11.95MB/s 0:00:45
这将输出和进度存储在proc变量中,如下所示:
proc = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', source12, destination],
stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
receiving incremental file list
rathi/
rathi/20090209.02s1.1_sequence.txt.gz
209,398,969 27% 11.95MB/s 0:00:45
我只想从上面的行中提取进度百分比。我想每1分钟检查一次变量,并提取更新的百分比。对于解析,我认为可以使用
re
模块。我怎样才能达到我的目标?谢谢您确实可以通过re获得进展
progress = re.search('\d*%',str).group()
其中str是要搜索的字符串
编辑:
你需要做一个while循环
while(progress < 101%):
get_new_progress
re.search....
while(进度<101%):
取得新的进展
搜索。。。。
请参见。没用!没有工作是毫无意义的反馈谢谢你的回答。是的,我拿到了百分比。如何检查每分钟的变量并每次获得更新的百分比?谢谢斯更新了我的答案,考虑了伪代码。您可以使用int()将进度转换为一个整数,以便在while循环中使用。是的,可以这样做!我会尽力让你知道的。谢谢