【发布时间】:2015-06-19 21:20:38
【问题描述】:
首先,我尝试了这些问题并没有为我工作:
- Python search and replace in binary file
- python re module to replace the binary data inside a text file?
我正在处理二进制格式的 pdf 文件。我需要用其他替换一个字符串。
这是我的方法:
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
with open("proof.pdf", "rb") as input_file:
content = input_file.read()
if b"21.8182 686.182 261.818 770.182" in content:
print("FOUND!!")
content.replace(b"21.8182 686.182 261.818 770.182", b"1.1 1.1 1.1 1.1")
if b"1.1 1.1 1.1 1.1" in content:
print("REPLACED!!")
with open("proof_output.pdf", "wb") as output_file:
output_file.write(content)
当我运行脚本时,它显示“FOUND!!”,而不是“REPLACED!!”
【问题讨论】:
标签: python python-3.x file-io replace binary