如何从文件中读取字节 [英] How to Read Bytes from a file

查看:71
本文介绍了如何从文件中读取字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来这很容易,但我正在画一个空白。


我想做的是能够以二进制模式打开任何文件,并阅读

一次一个字节(8位),然后计算

那个字节的1位数。


I得到了这个,但它给了我一些字符串,我不确定如何准确到达字节/位级别。


f1 = file(''somefile'',''rb'')

而1:

abyte = f1.read(1)


提前感谢您的帮助。


-Greg

It seems like this would be easy but I''m drawing a blank.

What I want to do is be able to open any file in binary mode, and read
in one byte (8 bits) at a time and then count the number of 1 bits in
that byte.

I got as far as this but it is giving me strings and I''m not sure how
to accurately get to the byte/bit level.

f1=file(''somefile'',''rb'')
while 1:
abyte=f1.read(1)

Thanks in advance for any help.

-Greg

推荐答案

gr********@gmail.com < gr ******** @ gmail.comwrote:
gr********@gmail.com <gr********@gmail.comwrote:

看起来这很容易,但我正在画一个空白。


什么我想做的是能够以二进制模式打开任何文件,并一次读取一个字节(8位)的
然后凑这个字节中的1比特数是多少。


我得到了这个但它给了我字符串我不知道怎么样

准确到达字节/位级别。


f1 = file(''somefile'',''rb'')

而1:

abyte = f1.read(1)
It seems like this would be easy but I''m drawing a blank.

What I want to do is be able to open any file in binary mode, and read
in one byte (8 bits) at a time and then count the number of 1 bits in
that byte.

I got as far as this but it is giving me strings and I''m not sure how
to accurately get to the byte/bit level.

f1=file(''somefile'',''rb'')
while 1:
abyte=f1.read(1)



你应该在循环前准备char的映射编号

的1位字符:


m = {}

范围内的c(256):

m [c] = countones(c)


然后将m [abyte]的值总结为一个运行总计(从
$ b中断当''不是abyte''时,$ b循环,即你正在读取0字节,即使

要求1 - 告诉你罚款完成,记得关闭

it。


完成countones功能的一个简单方法:


def countones(x):

断言x> = 0

c = 0

而x:

c + =(x& 1)

x>> = 1

返回c


你不想经常打电话,从那里以前的建议

只需要256次来准备映射。


如果下载并安装gmpy,你可以使用gmpy.popcount作为快速

计数器的实现:-)。

Alex

You should probaby prepare before the loop a mapping from char to number
of 1 bits in that char:

m = {}
for c in range(256):
m[c] = countones(c)

and then sum up the values of m[abyte] into a running total (break from
the loop when ''not abyte'', i.e. you''re reading 0 bytes even though
asking for 1 -- that tells you the fine is finished, remember to close
it).

A trivial way to do the countones function:

def countones(x):
assert x>=0
c = 0
while x:
c += (x&1)
x >>= 1
return c

you just don''t want to call it too often, whence the previous advice to
call it just 256 times to prep a mapping.

If you download and install gmpy you can use gmpy.popcount as a fast
implementation of countones:-).
Alex


Alex Martelli写道:
Alex Martelli wrote:

你应该在循环之前准备一个从char到数字的映射

的1比特:
m = { }

范围内的c(256):

m [c] = countones(c)
You should probaby prepare before the loop a mapping from char to number
of 1 bits in that char:

m = {}
for c in range(256):
m[c] = countones(c)



Wouldn 列表更有效率吗?


m = [x(256)中c的countones(c)

Wouldn''t a list be more efficient?

m = [countones(c) for c in xrange(256)]


On 3月1日上午7:52,gregpin ... @ gmail.com < gregpin ... @ gmail.com>

写道:
On Mar 1, 7:52 am, "gregpin...@gmail.com" <gregpin...@gmail.com>
wrote:

看起来这很容易但我正在画画空白。


我想要做的是能够以二进制模式打开任何文件,并在一个字节(8位)中读取
时间,然后计算

那个字节的1位数。


我得到了这个,但它给了我一些字符串,我是不确定

如何准确到达字节/位级别。


f1 = file(''somefile'',''rb'')

而1:

abyte = f1.read(1)
It seems like this would be easy but I''m drawing a blank.

What I want to do is be able to open any file in binary mode, and read
in one byte (8 bits) at a time and then count the number of 1 bits in
that byte.

I got as far as this but it is giving me strings and I''m not sure how
to accurately get to the byte/bit level.

f1=file(''somefile'',''rb'')
while 1:
abyte=f1.read(1)



import struct

buf = open(''somefile'',''rb'')。read()

count1 = lambda x:(x& 1)+(x& 2> 0)+( x& 4> 0)+(x& 8> 0)+(x& 16> 0)+(x& 32> 0)+

(x& 64> 0)+(x& 128> 0)

byteOnes = map(count1,struct.unpack(''B''* len(buf),buf))


byteOnes [ n]是数字是n字节n中的数字。

import struct
buf = open(''somefile'',''rb'').read()
count1 = lambda x: (x&1)+(x&2>0)+(x&4>0)+(x&8>0)+(x&16>0)+(x&32>0)+
(x&64>0)+(x&128>0)
byteOnes = map(count1,struct.unpack(''B''*len(buf),buf))

byteOnes[n] is number is number of ones in byte n.


这篇关于如何从文件中读取字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆