Python 去掉字节 b' '
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17013089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python get rid of bytes b' '
提问by
import save
string = ""
with open("image.jpg", "rb") as f:
byte = f.read(1)
while byte != b"":
byte = f.read(1)
print ((byte))
I'm getting bytes like:
我得到的字节如下:
b'\x00'
b'\x00'
How do I get rid of this b''?
我该如何摆脱这个b''?
Let's say I wanna save the bytes to a list, and then save this list as the same image again. How do I proceed?
假设我想将字节保存到列表中,然后再次将此列表保存为相同的图像。我该如何进行?
Thanks!
谢谢!
回答by jureslak
The b"..." is just a python notation of byte strings, it's not really there, it only gets printed. Does it cause some real problems to you?
b"..." 只是字节字符串的 python 符号,它并不存在,它只是被打印出来。它会给你带来一些实际的问题吗?
回答by mishik
You can use bytes.decode function if you really need to "get rid of b": http://docs.python.org/3.3/library/stdtypes.html#bytes.decode
如果你真的需要“摆脱 b”,你可以使用 bytes.decode 函数:http://docs.python.org/3.3/library/stdtypes.html#bytes.decode
But it seems from your code that you do not really need to do this, you really need to work with bytes.
但是从您的代码看来,您实际上并不需要这样做,您确实需要使用字节。
回答by Fredrik Pihl
To operate on binary data you can use the array-module. Below you will find an iterator that operates on 4096 chunks of data instead of reading everything into memory at ounce.
要对二进制数据进行操作,您可以使用array-module。您将在下面找到一个迭代器,它对 4096 块数据进行操作,而不是以盎司为单位将所有内容读入内存。
import array
def bytesfromfile(f):
while True:
raw = array.array('B')
raw.fromstring(f.read(4096))
if not raw:
break
yield raw
with open("image.jpg", 'rb') as fd
for byte in bytesfromfile(fd):
for b in byte:
# do something with b
回答by janos
This is one way to get rid of the b'':
这是摆脱 的一种方法b'':
import sys
print(b)
If you want to save the bytes later it's more efficient to read the entire file in one go rather than building a list, like this:
如果您想稍后保存字节,则一次性读取整个文件比构建列表更有效,如下所示:
with open('sample.jpg', mode='rb') as fh:
content = fh.read()
with open('out.jpg', mode='wb') as out:
out.write(content)
回答by nmclean
The b'', is only the string representation of the data that is written when you printit.
的b'',仅仅是写在你的数据的字符串表示print的。
Using decodewill not help you here because you only want the bytes, not the characters they represent. Slicing the string representation will help even less because then you are still left with a string of several useless characters ('\', 'x', and so on), not the original bytes.
使用decode在这里对您没有帮助,因为您只需要字节,而不是它们代表的字符。对字符串表示进行切片将更没有帮助,因为那样您仍然会留下几个无用字符('\'、'x' 等)的字符串,而不是原始字节。
There is no need to modify the string representation of the data, because the data is still there. Just use it instead of the string (i.e. don't use print). If you want to copy the data, you can simply do:
不需要修改数据的字符串表示,因为数据还在。只需使用它而不是字符串(即不要使用print)。如果要复制数据,只需执行以下操作:
data = file1.read(...)
...
file2.write(data)
If you want to output the binary data directly from your program, use the sys.stdout.buffer:
如果要直接从程序中输出二进制数据,请使用sys.stdout.buffer:
import sys
sys.stdout.buffer.write(data)
回答by dhvlnyk
Here is one solution
这是一种解决方案
print(str(byte[2:-1]))
print(str(byte[2:-1]))

