python - ZipReplaceData.py ReplaceString -
how replace string instead of replacing entire content of hello.txt?
is possible?
it needs {
findstring('hello');
replacestring('hello', 'goodbye');
}
# file: zipreplacedata.py
import chilkat # open zip, locate file contained within it, replace # contents of file, , save zip. zip = chilkat.ckzip() zip.unlockcomponent("anything 30-day trial") success = zip.openzip("exampledata.zip") if success: # zip in example contains these files , directories: # exampledata\ # exampledata\hamlet.xml # exampledata\123\ # exampledata\aaa\ # exampledata\123\hello.txt # exampledata\aaa\banner.gif # exampledata\aaa\dude.gif # exampledata\aaa\xyz\ # forward , backward slashes equivalent , either can used.. zipentry = zip.firstmatchingentry("*/hello.txt") if (zipentry != none): # replace contents of hello.txt else. newcontent = chilkat.ckstring() newcontent.append("goodbye!") zipentry.replacedata(newcontent) # save zip new content. zip.writezipandclose() else: print "failed find hello.txt!\n" else: # failed open .zip archive. zip.savelasterror("openziperror.txt")
you may use string.replace() method job.
sample code: following code snippet replace occurrences of hello
bye
.
mystring = 'hello there! hello again.' print mystring.replace('hello', 'bye')
output: bye there! hello again.
ps: above code replace hello
word in h
capital. if want replace irrespective of upper case/lower case letters may first convert letters either upper case or lower case , use replace()
.
sample code: code replace occurrences of hello
word bye
.
mystring = 'hello there! hello again.' mystring = mystring.lower()# convert lower case print mystring.replace('hello', 'bye')
output: bye there! bye again.
i hope helpful.
Comments
Post a Comment