如何替换字符串而不是替换整个字符串 hello.txt的内容?
这可能吗?
需要{
FINDSTRING('你好&#39);
ReplaceString(' Hello',' Goodbye');
}
#file:ZipReplaceData.py
import chilkat
# Open a zip, locate a file contained within it, replace the
# contents of the file, and save the zip.
zip = chilkat.CkZip()
zip.UnlockComponent("anything for 30-day trial")
success = zip.OpenZip("exampleData.zip")
if success:
# The zip in this example contains these files and 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 and backward slashes are equivalent and either can be used..
zipEntry = zip.FirstMatchingEntry("*/hello.txt")
if (zipEntry != None):
# Replace the contents of hello.txt with something else.
newContent = chilkat.CkString()
newContent.append("Goodbye!")
zipEntry.ReplaceData(newContent)
# Save the Zip with the new content.
zip.WriteZipAndClose()
else:
print "Failed to find hello.txt!\n"
else:
# Failed to open the .zip archive.
zip.SaveLastError("openZipError.txt")
答案 0 :(得分:0)
您可以使用string.replace()方法完成此项工作。
示例代码:以下代码段会将Hello
的所有匹配项替换为Bye
。
myString = 'Hello there! hello again.'
print myString.replace('Hello', 'bye')
输出:bye there! hello again.
PS:上述代码只会替换Hello
为资本的H
字词。如果您想要替换大小写/小写字母,那么您可以先将所有字母转换为大写或小写,然后使用replace()
。
示例代码:此代码会将hello
字的所有匹配项替换为Bye
。
myString = 'Hello there! hello again.'
myString = myString.lower()# Convert to lower case
print myString.replace('hello', 'bye')
输出:bye there! bye again.
我希望它有所帮助。