[英]Write string of escaped byte characters, ie '\x00\x20\…', as if they were bytes
我正在玩.wav文件生成,并具有将基数10的整数转换为十六进制的函数。 我需要将这些十六进制数字写为字节,而不是简单地将它们编码为字节。 我可以使用转义序列进行硬编码,但是我需要使用函数提供的字符串。
我的临时工作如下使用eval。
eval("b'" + endian(bytestring(tohex(16, 32))) + "'")
在没有评估的情况下完成此任务的更优选方法是什么?
这里有一些代码可以更好地了解情况。
with open('song.wav', 'wb') as f:
f.write(ChunkID)
f.write(ChunkSize)
f.write(Format)
f.write(Subchunk1ID)
f.write(Subchunk1Size)
f.write(AudioFormat)
f.write(NumChannels)
f.write(SampleRate)
f.write(ByteRate)
f.write(BlockAlign)
f.write(BitsPerSample)
f.write(Subchunk2ID)
f.write(Subchunk2Size)
f.write(Data)
使用硬编码值可以正常工作。
ChunkID = b'RIFF' #big 1
ChunkSize = b'\x1a\xea\xc4\x00' #little 2
Format = b'WAVE' #big 3
Subchunk1ID = b'fmt ' #big 4
Subchunk1Size = b'\x10\x00\x00\x00' #little 5
AudioFormat = b'\x01\x00' #little 6
NumChannels = b'\x02\x00' #little 7
SampleRate = b'\x22\x56\x00\x00' #little 8
ByteRate = b'\x88\x58\x01\x00' #little 9
BlockAlign = b'\x04\x00' #little 10
BitsPerSample = b'\x10\x00' #little 11
Subchunk2ID = b'data' #big 12
Subchunk2Size = b'\x10\xb1\x02\x00' #little 13
Data = b'' #little 14
我需要更大的灵活性,并且需要使用函数获得相似的结果。
def tohex(val, nbits):
a = hex((val + (1 << nbits)) % (1 << nbits))
pad = nbits/4 - (len(a) - 2)
for i in range(0, pad):
a = a[:2] + '0' + a[2:]
return a
def bytestring(hexstring):
hexstring = hexstring[2:]
bstring = ''
for i in range(0, len(hexstring)/2):
bstring += '\\x'
bstring += hexstring[2*i: 2*i+2]
return bstring
def endian(bstring):
return '\\'+'\\'.join(reversed(bstring.split('\\'))).rstrip('\\')
这就是我所谓的函数
sample_rate = 44100
channels = 2
sample_bits = 16
sample_count = 176400
ChunkID = b'RIFF'
Format = b'WAVE'
Subchunk1ID = b'fmt '
Subchunk1Size = eval("b'" + endian(bytestring(tohex(16, 32))) + "'") #n bit samples : tohex(n,m)
AudioFormat = eval("b'" + endian(bytestring(tohex(1, 16))) + "'") #uncompressed when n=1
NumChannels = eval("b'" + endian(bytestring(tohex(channels, 16))) + "'") #n channels
SampleRate = eval("b'" + endian(bytestring(tohex(sample_rate, 32))) + "'")
BitsPerSample = eval("b'" + endian(bytestring(tohex(sample_bits, 16))) + "'")
ByteRate = eval("b'" + endian(bytestring(tohex(sample_rate*channels*sample_bits/8, 32))) + "'")
BlockAlign = eval("b'" + endian(bytestring(tohex(channels*sample_bits/8, 16))) + "'")
Subchunk2ID = b'data'
Subchunk2Size = eval("b'" + endian(bytestring(tohex(sample_count*channels*sample_bits/8, 32))) + "'")
ChunkSize = eval("b'" + endian(bytestring(tohex(36 + sample_count*channels*sample_bits/8, 32))) + "'")
Data = b''
for i in range(0, 176400): #Just test samples
if i % 32 < 16:
Data += b'\x10\xe7\x10\xe7'
else:
Data += b'\xf0\xa8\xf0\xa8'
更新:我添加了一个新功能来处理以前的格式并返回可以直接写入文件的表格。 我标记的解决方案使用字节,但是在python 2.7中,我不得不使用bytearray。
def barray(bstring):
hexmap = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7,'8':8, '9':9, 'a':10, 'b':11, 'c':12, 'd':13, 'e':14, 'f':15}
bstring = bstring.lstrip('\\x')
bstring = bstring.split('\\x')
bstring = map(lambda x: 16*hexmap[x[0]]+hexmap[x[1]], bstring)
return bytearray(bstring)
现在,在调用函数之后,无需使用eval()就可以将数据写入文件了。
variable = barray(endian(bytestring(tohex(n, m))))
您尝试使用bytes
吗?
data = '\x00\x20\x10'
newdata = [int(i) for i in data.encode()]
print(newdata)
>>> [0, 32, 16]
print(bytes(newdata))
>>> b'\x00 \x10' # it only looks like the middle value is missing, it isn't
print([i for i in b'\x00 \x10'])
>>> [0, 32, 16] # yep, all still there
在澄清问题后,根据对问题的更好理解进行编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.