我发现使用Python读取二进制文件特别困难.你能帮我个忙吗?我需要阅读这个文件,它在Fortran 90中很容易阅读
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)
Run Code Online (Sandbox Code Playgroud)
具体来说,文件格式为:
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.
Run Code Online (Sandbox Code Playgroud)
我怎么用Python阅读?我尝试了一切,但从未奏效.我有没有机会在python中使用f90程序,读取这个二进制文件,然后保存我需要使用的数据?
我正在尝试逐字节读取文件,但我不知道该怎么做.我试着这样做:
file = open(filename, 'rb')
while 1:
byte = file.read(8)
# Do something...
Run Code Online (Sandbox Code Playgroud)
那么这会使变量字节在每个循环开始时包含8个下一位吗?这些字节究竟是什么并不重要.唯一重要的是我需要读取8位堆栈中的文件.
编辑:
此外,我在列表中收集这些字节,我想打印它们,以便它们不打印为ASCII字符,但作为原始字节,即当我打印该bytelist时,它将结果显示为
['10010101', '00011100', .... ]
Run Code Online (Sandbox Code Playgroud) 在Python中,对于二进制文件,我可以这样写:
buf_size=1024*64 # this is an important size...
with open(file, "rb") as f:
while True:
data=f.read(buf_size)
if not data: break
# deal with the data....
Run Code Online (Sandbox Code Playgroud)
有了我想逐行阅读的文本文件,我可以这样写:
with open(file, "r") as file:
for line in file:
# deal with each line....
Run Code Online (Sandbox Code Playgroud)
这是简写:
with open(file, "r") as file:
for line in iter(file.readline, ""):
# deal with each line....
Run Code Online (Sandbox Code Playgroud)
这个成语记录在PEP 234中,但我找不到二进制文件的类似习惯用法.
我试过这个:
>>> with open('dups.txt','rb') as f:
... for chunk in iter(f.read,''):
... i+=1
>>> i
1 # 30 MB file, …Run Code Online (Sandbox Code Playgroud) Python 3.我正在使用QT的文件对话框小部件来保存从互联网下载的PDF.我一直在使用'open'读取文件,并尝试使用文件对话框小部件来编写它.但是,我一直遇到"TypeError:'_ io.BufferedReader'不支持缓冲区接口"错误.
示例代码:
with open('file_to_read.pdf', 'rb') as f1:
with open('file_to_save.pdf', 'wb') as f2:
f2.write(f1)
Run Code Online (Sandbox Code Playgroud)
当不使用'b'指示符时,或者当从web读取文件时,这种逻辑适用于文本文件,例如urllib或者请求.这些是"字节"类型,我认为我需要打开文件.相反,它作为缓冲读者开放.我尝试了字节(f1),但得到"TypeError:'bytes'对象不能被解释为整数." 有什么想法吗?
我想从一个文件中读取一个数字列表,一次只能查询一个字符,以检查该字符是什么,无论是数字,句号,+或 - ,e还是E,还是其他一些字符. ..然后根据它执行我想要的任何操作.如何使用我已有的现有代码执行此操作?这是我尝试过的一个例子,但没有用.我是python的新手.提前致谢!
import sys
def is_float(n):
state = 0
src = ""
ch = n
if state == 0:
if ch.isdigit():
src += ch
state = 1
...
f = open("file.data", 'r')
for n in f:
sys.stdout.write("%12.8e\n" % is_float(n))
Run Code Online (Sandbox Code Playgroud) 我需要从Python导入一个二进制文件 - 内容是带符号的16位整数,大端.
以下Stack Overflow问题建议如何一次拉入几个字节,但这是扩展读取整个文件的方法吗?
我想创建一个像以下的函数:
from numpy import *
import os
def readmyfile(filename, bytes=2, endian='>h'):
totalBytes = os.path.getsize(filename)
values = empty(totalBytes/bytes)
with open(filename, 'rb') as f:
for i in range(len(values)):
values[i] = struct.unpack(endian, f.read(bytes))[0]
return values
filecontents = readmyfile('filename')
Run Code Online (Sandbox Code Playgroud)
但这很慢(文件是165924350字节).有没有更好的办法?
以下代码似乎没有正确读/写二进制形式.它应该读取一个二进制文件,逐位XOR数据并将其写回文件.没有任何语法错误,但数据无法验证,我已通过其他工具测试源数据以确认xor密钥.
更新:根据评论中的反馈,这很可能是由于我正在测试的系统的字节序.
def four_byte_xor(buf, key):
out = ''
for i in range(0,len(buf)/4):
c = struct.unpack("=I", buf[(i*4):(i*4)+4])[0]
c ^= key
out += struct.pack("=I", c)
return out
Run Code Online (Sandbox Code Playgroud)
调用xortools.py:
from xortools import four_byte_xor
in_buf = open('infile.bin','rb').read()
out_buf = open('outfile.bin','wb')
out_buf.write(four_byte_xor(in_buf, 0x01010101))
out_buf.close()
Run Code Online (Sandbox Code Playgroud)
看来我需要读取每个答案的字节数.由于上面的函数操作多个字节,上面的函数如何合并到下面?或者没关系?我需要使用struct吗?
with open("myfile", "rb") as f:
byte = f.read(1)
while byte:
# Do stuff with byte.
byte = f.read(1)
Run Code Online (Sandbox Code Playgroud)
例如,以下文件有4个重复字节,01020304:

使用01020304的密钥对数据进行异或操作,该密钥将原始字节归零:

这是对原始函数的尝试,在这种情况下,05010501的结果是不正确的:

我想读取文件的原始二进制文件并将其放入字符串中.目前我打开一个带有"rb"标志的文件并打印字节,但它会以ASCII字符形式出现(对于文本来说,对于视频和音频文件,它会给出符号和乱码).如果可能的话,我想获得原始的0和1.这需要适用于音频和视频文件,因此简单地将ascii转换为二进制文件不是一种选择.
file = open(filePath, "rb")
with file:
byte = file.read(1)
print byte
Run Code Online (Sandbox Code Playgroud) 我正在处理提供的数据在.data文件中的辅助内容。如何打开.data文件以查看数据的外观以及如何.data通过 python以编程方式读取文件?我有 Mac OSX
注意:我正在处理的数据是针对其中一个KDD cup challenges
最近我在学习python,然后我意识到python文档中存在一个read1()。我想知道 read() 和 read1() 之间有什么区别?什么情况下我们应该使用 read1() 而不是 read()?