我發現使用Python讀取二進制文件特別困難。你能幫我一下嗎? 我需要讀取這個文件,這在Fortran 90中很容易被用python讀取二進制文件
int*4 n_particles, n_groups
real*4 group_id(n_particles)
read (*) n_particles, n_groups
read (*) (group_id(j),j=1,n_particles)
詳細閱讀,文件格式爲:
Bytes 1-4 -- The integer 8.
Bytes 5-8 -- The number of particles, N.
Bytes 9-12 -- The number of groups.
Bytes 13-16 -- The integer 8.
Bytes 17-20 -- The integer 4*N.
Next many bytes -- The group ID numbers for all the particles.
Last 4 bytes -- The integer 4*N.
我如何使用Python閱讀本?我嘗試了一切,但沒有奏效。是否有機會在python中使用f90程序,讀取這個二進制文件,然後保存我需要使用的數據?
當時這個文件寫的Fortran程序?如果是這樣,它是如何寫入的,因爲默認情況下,Fortran會在寫入文件的每個記錄之前添加其他數據。讀取數據時可能需要注意這一點。 – Chris 2012-01-03 10:02:46
請忽略我之前的評論,整數8和4 * N顯然是這些額外的數據。 – Chris 2012-01-03 10:43:33
此外,請參閱問題的答案[在Python中讀取二進制文件](http://stackoverflow.com/questions/1035340/reading-binary-file-in-python)。 – Chris 2012-01-03 10:46:53