简体   繁体   English

如何使用python将字符串替换为gzip文件中的另一个字符串?

[英]How can I replace string with another string in a gzip file by using python?

I am trying to find the specific string in gzip, replace it with another string and update the gzip file. 我正在尝试在gzip中找到特定的字符串,将其替换为另一个字符串并更新gzip文件。

Here is my python script. 这是我的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

How can I replace the specific string and write it? 如何替换特定的字符串并将其写入?

Actually I want to update the html tag of method definition with hyperlink title and red color 其实我想用超链接标题和红色更新方法定义的html标签

Before 之前

<a class="l" name="5132" href="#5132">5132</a>    <b>void</b> <a class="xmt" name="setHdmiPlugged"/><a href="/source/s?refs=setHdmiPlugged&amp;project=android" class="xmt">setHdmiPlugged</a>(<b>boolean</b> <a class="xa" name="plugged"/><a href="/source/s?refs=plugged&amp;project=android" class="xa">plugged</a>) {

After

<a class="l" name="5132" href="#5132">5132</a>    <b>void</b> <a class="xmt" name="setHdmiPlugged"/><a href="/source/s?refs=setHdmiPlugged&amp;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&amp;project=android" class="xa">plugged</a>) {

EDIT 编辑

I completed this code. 我完成了这段代码。 Please refer to the below code if you needed. 如果需要,请参考以下代码。

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()

This is one way to replace a specific string : 这是替换特定字符串的一种方法:

a = 'string 1'
b = 'string 2'
a = a.replace(a,b)

When you print a the output is: 当您打印a输出为:

string 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中替换字符串中的字符,但不使用字符串方法? - How can I replace characters in a string in Python, but not using string methods? 如何在 python 中使用 gzip 将 header 添加到压缩字符串中? - How can I add header to compressed string with gzip in python? 如何打开文件,替换该文件中的字符串,然后使用Python将其写入新文件 - How can I open a file, replace a string in that file and write it to a new file using Python 我如何 gzip 压缩 Python 中的字符串? - How do I gzip compress a string in Python? 如何使用python替换字符串中的多个字符? - How can I replace multiple characters in a string using python? 使用python finditer,我该如何替换每个匹配的字符串? - using python finditer, how can I replace each matched string? 用Python文件中的另一个字符串替换一个字符串 - Replace a string with another string within a file in Python 如何使用python 2.7用另一个文本/字符串替换文件中的文本/字符串 - How to replace a text/string in a file with another text/string using python 2.7 如何使用python在文件中搜索字符串(比如str1)并将其替换为另一个字符串(比如str2)? - How to search a string(say str1) and replace it with another string(say str2) in a file using python? 如何在 Python 中的字符串中替换 =\\ - How can I replace =\ in a string in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM