简体   繁体   English

python将读取文件中的字符串方法替换为二进制

[英]python replace string method in read file as binary

I opened an image file in readbinary("rb") mode and stored the data in a variable. 我以readbinary(“ rb”)模式打开图像文件,并将数据存储在变量中。 Now i want to replace some values in the binary with my values.. but its not working using usual replace method of string 现在我想用我的值替换二进制文件中的一些值..但是使用字符串的常规替换方法无法正常工作

f=open("a.jpg","rb")
a=f.read()

''' first line is '\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xe1\x00*Exif\x00\x00II*\x00\x08\x00\x00\x00\x0 '''

a=a.replace("ff","z")
print a

#but there's no change in a

can anyone tell where iam going wrong.. i also tried 谁能说出iam出了错..我也试过了

a=a.replace(b'ff',b'z')

but still the output was unchanged. 但输出仍保持不变。

can anyone tell what iam supposed to do to perform the replacement? 谁能告诉我我应该做的替换工作?

I don't know which version of Python you're using (this kind of operations are different between 2 and 3), but try a = str(a) before executing replace method. 我不知道您使用的是哪个Python版本(这种操作在2和3之间是不同的),但是在执行replace方法之前尝试使用a = str(a)

EDIT: For python 2.7 only reasonable way I've discovered to do what you want is use built-in function repr . 编辑:对于python 2.7我发现做你想要的唯一合理的方法是使用内置函数repr Example: 例:

>>> picture = open("some_picture.jpg", 'rb')
>>> first_line = picture.readline()
>>> first_line
'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xe1\x00*Exif\x00\x00II*\x00\x08\x00\x00\x00\x01\x001\x01\x02\x00\x07\x00\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00Google\x00\x00\xff\xdb\x00\x84\x00\x03\x02\x02\x03\x02\x02\x03\x03\x03\x03\x04\x03\x03\x04\x05\x08\x05\x05\x04\x04\x05\n'
>>> repr(first_line)
>>> "'\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x01\\x01\\x00\\x00\\x01\\x00\\x01\\x00\\x00\\xff\\xe1\\x00*Exif\\x00\\x00II*\\x00\\x08\\x00\\x00\\x00\\x01\\x001\\x01\\x02\\x00\\x07\\x00\\x00\\x00\\x1a\\x00\\x00\\x00\\x00\\x00\\x00\\x00Google\\x00\\x00\\xff\\xdb\\x00\\x84\\x00\\x03\\x02\\x02\\x03\\x02\\x02\\x03\\x03\\x03\\x03\\x04\\x03\\x03\\x04\\x05\\x08\\x05\\x05\\x04\\x04\\x05\\n'"
>>> repr(first_line).replace('ff', 'SOME_OTHER_STRING')
"'\\xSOME_OTHER_STRING\\xd8\\xSOME_OTHER_STRING\\xe0\\x00\\x10JFIF\\x00\\x01\\x01\\x00\\x00\\x01\\x00\\x01\\x00\\x00\\xSOME_OTHER_STRING\\xe1\\x00*Exif\\x00\\x00II*\\x00\\x08\\x00\\x00\\x00\\x01\\x001\\x01\\x02\\x00\\x07\\x00\\x00\\x00\\x1a\\x00\\x00\\x00\\x00\\x00\\x00\\x00Google\\x00\\x00\\xSOME_OTHER_STRING\\xdb\\x00\\x84\\x00\\x03\\x02\\x02\\x03\\x02\\x02\\x03\\x03\\x03\\x03\\x04\\x03\\x03\\x04\\x05\\x08\\x05\\x05\\x04\\x04\\x05\\n'"

When you display a string at the Python console, the string is encoded so that you can see all of the characters, even the ones that aren't printable. 在Python控制台上显示字符串时,该字符串已编码,因此您可以看到所有字符,甚至是无法打印的字符。 Whenever you see something like \\xff , that's not 4 characters, it's a single character in hex notation. 每当您看到\\xff之类的不是4个字符的东西时,它都是十六进制表示法中的单个字符 To replace it, you also need to specify the same single character. 要替换它,您还需要指定相同的单个字符。

a = a.replace("\xff", "z")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM