我正在尝试在gzip中找到特定字符串,将其替换为另一个字符串并更新gzip文件。
这是我的python脚本。
import gzip
import string
lines = gzip.open("PhoneWindowManager.java.gz", "rb")
definition = "name="+'"setHdmiPlugged"'+"/>"
print definition
for line in lines:
if definition in line:
before = ">"+"setHdmiPlugged"+"</a>"
print before
after = " title="+'"Join"'+"><font color="+'"red"'">"+"setHdmiPlugged"+"</font></a>"
print after
new_str = string.replace(line, before, after)
print new_str
**** How can I update the gzip file with the new_str? ****
break
如何替换特定字符串并写入?
实际上我想用超链接标题和红色
更新方法定义的html标签在
<a class="l" name="5132" href="#5132">5132</a> <b>void</b> <a class="xmt" name="setHdmiPlugged"/><a href="/source/s?refs=setHdmiPlugged&project=android" class="xmt">setHdmiPlugged</a>(<b>boolean</b> <a class="xa" name="plugged"/><a href="/source/s?refs=plugged&project=android" class="xa">plugged</a>) {
在
<a class="l" name="5132" href="#5132">5132</a> <b>void</b> <a class="xmt" name="setHdmiPlugged"/><a href="/source/s?refs=setHdmiPlugged&project=android" class="xmt" title="Test"><font color="red">setHdmiPlugged</font></a>(<b>boolean</b> <a class="xa" name="plugged"/><a href="/source/s?refs=plugged&project=android" class="xa">plugged</a>) {
修改
我完成了这段代码。如果需要,请参阅以下代码。
import gzip
import string
import os
os.rename("PhoneWindowManager.java.gz","PhoneWindowManager_orig.java.gz")
input = gzip.open("PhoneWindowManager_orig.java.gz", "rb")
output = gzip.open("PhoneWindowManager.java.gz", "wb")
definition = "name="+'"setHdmiPlugged"'+"/>"
for line in input:
if definition in line:
before = ">"+"setHdmiPlugged"+"</a>"
after = " title="+'"Join"'+"><font color="+'"red"'">"+"setHdmiPlugged"+"</font></a>"
new_str = string.replace(line, before, after)
output.write(new_str)
else:
output.write(line)
input.close()
output.close()
答案 0 :(得分:0)
这是替换特定字符串的一种方法:
a = 'string 1'
b = 'string 2'
a = a.replace(a,b)
当您打印a
时,输出为:
string 2