用python过滤输出?

用python过滤输出?,python,Python,我有一个python脚本,它使用mechanize从网页获取数据。这工作很好,但我已经做了一项黑客工作,然后使用bash过滤我要查找的文本。我现在需要在主python脚本中执行此操作,因为我需要使用输出值 response = br.submit() print response.read() 这将打印出响应,然后使用bash进行操作 | grep usedData | cut -d '"' -f2 | sed 's/\<GB used\>//g'` | grep usedDat

我有一个python脚本,它使用mechanize从网页获取数据。这工作很好,但我已经做了一项黑客工作,然后使用bash过滤我要查找的文本。我现在需要在主python脚本中执行此操作,因为我需要使用输出值

response = br.submit()
print response.read()
这将打印出响应,然后使用bash进行操作

| grep usedData | cut -d '"' -f2 | sed 's/\<GB used\>//g'`
| grep usedData | cut-d'-f2 | sed's/\//g'`
如何在python中完成这一切

bash脚本的输出将是一个数字(例如123.45)

输入:

<tr><th>Current Data Usage:  </th><td>221.59GB</td></tr>


Output: 221.59
当前数据使用量:221.59GB
产出:221.59

试试这个:

input_html = "<tr><th>Current Data Usage:  </th><td>221.59GB</td></tr>"
begin = input_html.find("</th><td>")
end = input_html.find("GB</td>")
output = input_html[begin+len("</th><td>"):end]
print output
input\u html=“当前数据使用量:221.59GB”
begin=input\u html.find(“”)
end=input_html.find(“GB”)
输出=输入\u html[开始+长度(“”:结束]
打印输出

这应该可以准确地找到您要查找的内容。

您可以使用正则表达式查找“GB”之前的所有数字和句点序列

>>重新导入
>>>s=“当前数据使用量:221.59GB”
>>>匹配=重新搜索(r“([\d\.]*)GB”,s)
>>>匹配组(1)
'221.59'

请给出一些问题的输入/输出示例。您可以通过stdin参数使用python;请不要使用
str
作为变量。如果这样做,您将无法使用
str()
function。很抱歉告诉您,但是
input
在python中也是一个函数;-)我也刚刚修复了;)非常感谢-我喜欢这个解决方案。似乎比我的bash脚本简单多了:)
>>> import re
>>> s = "<tr><th>Current Data Usage:  </th><td>221.59GB</td></tr>"
>>> match = re.search(r"([\d\.]*)GB", s)
>>> match.group(1)
'221.59'