1. 程式人生 > >python 5-2-2 如何使用array.array('h',(0 for _ in xrange(n)))/buf.tofile(f2)

python 5-2-2 如何使用array.array('h',(0 for _ in xrange(n)))/buf.tofile(f2)

python 5-2-2 如何使用array.array(‘h’,(0 for _ in xrange(n)))/buf.tofile(f2)

import array

f = open(“d:\demo.wav”,”rb”) ==>通過二進位制開啟檔案
info = f.read(44) ==>讀取檔案前44個位元組
f.seek(0,2) ==>將檔案指標直到檔案末尾
n = (f.tell() - 44)/2 ==>f.tell()獲得檔案長度
buf = array.array(‘h’,(0 for _ in xrange(n))) ==>初始化陣列
f.seek(44) ==>檔案指標指到第44個位元組
f.readinto(buf) ==>將檔案44個位元組以後的內容寫入到buf中
f.close() ==>

for i in xrange(n):buf[i] /= 8 ==》將每個數字除以8
f2 = open(“d:\test2.wav”,”wb”)
f2.write(info) ==>先將檔案的前44個位元組寫入到檔案,
buf.tofile(f2) ==》將修改過的檔案寫入到新檔案

f2.close()

help(buf.tofile)

help(buf.tofile)
Help on built-in function tofile:

tofile(…)
tofile(f)

Write all items (as machine values) to the file object f.  Also called as
write.

>
>

f = open(“d:\demo.wav”,”rb”)
help(f.readinto)
Help on built-in function readinto:

readinto(…)
readinto() -> Undocumented. Don’t use this; it may go away.

>