如何使用字符编码在文件中存储随机字节?

2024-05-29 04:10:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在Python3(使用Windows7)上运行其他人的Python2程序。它的目的是生成大阶乘,然后将它们用作随机数流。程序将十进制阶乘转换为0到255之间的字节值,并将chr(byte value)写入文件。它通过在8位小数的部分中移动阶乘来计算每个字节。但是,编码从Python2更改为3(我不确定它到底是什么或为什么重要),并且chr()命令对于128到159之间的任何值都不起作用(但是值160到255起作用)-程序将引发“UnicodeEncodeError: 'charmap' codec can't encode character '(the character point)' in position 0: character maps to <undefined>

我尝试用“open(filename, "w", encoding="utf-8")”更改文件编码,这成功地写入了所有字节。然而,当我测试文件的随机性属性时,它们明显比作者得到的结果差。你知道吗

我应该改变什么来存储字符字节而不影响数据的随机性?

这个测试程序被称为“ent”。在命令提示符下,它接受一个文件作为参数,然后输出一些随机性统计数据。欲了解更多信息,请访问其网站http://www.fourmilab.ch/random/。你知道吗

  • 我的耳鼻喉科结果文件来自!500000,使用open(filename, "w", encoding="utf-8")

    Entropy = 6.251272 bits per byte.
    
    Optimum compression would reduce the size of this 471812 byte file by 21 percent.
    
    Chi square distribution for 471812 samples is 6545600.65, and randomly
    would exceed this value less than 0.01 percent of the times.
    
    Arithmetic mean value of data bytes is 138.9331 (127.5 = random).
    Monte Carlo value for Pi is 3.173294335 (error 1.01 percent).
    Serial correlation coefficient is 0.162915 (totally uncorrelated = 0.0).
    
  • 作者对来自的文件的ent结果!50万:

    Entropy = 7.999373 bits per byte.
    
    Optimum compression would reduce the size of this 313417 byte file by 0 percent.
    
    Chi square distribution for 31347 samples is 272.63, and randomly would
    exceed this value 25.00 percent of the times.
    
    Arithmetic mean value of data bytes is 127.6336 (127.5 = random).
    Monte Carlo value for Pi is 3.149475458 (error 0.25 percent).
    Serial correlation coefficient is -0.001209 (totally uncorrelated = 0.0).
    

Tags: 文件ofthe程序for字节isvalue
2条回答

这里有一个示例(在python3中):

# check if the characters are matching Unicode
l1 = [chr(i) for i in range(128, 160)]
print("{}\n".format(l1))

s1 = " ".join(l1)

# display these characters for visual comparison
# before writing them to file
print("INITIAL:")
print(s1)

pf = open("somefile", "wb")
pf.write(s1.encode("utf-8"))
pf.close()

po = open("somefile", "rb")
out = po.read()
po.close()

s2 = out.decode('utf-8')

# display these characters for visual comparison    
# after writing them to file and reading them from it
print("AFTER:")
print(s2)  

其中我们测试了两个理论:

  • 字符(128到159)能被编码吗
  • 我们能把所有的数据以二进制形式写入一个文件吗?你知道吗

在第一个演示中,我们可以清楚地看到数据在Unicode字符映射中确实匹配。你知道吗

至于第二种理论,我们可以很明显地以原始形式写入和检索二进制数据,正如输出所示:

output

看起来timakro得到了答案(谢谢):

“要编写二进制文件,您应该以二进制模式打开它(文件名,“wb”),并向它写入类似字节的对象。例如,要写入值为123的字节:文件.write(字节([123]))。“-timakro

当我将“bytes([byte value from 0-255])”写入文件时,它会得到ent程序所期望的随机性分数。因此,我将python2的chr()更改为bytes(),以便程序在python3中存储字节。不需要字符编码。你知道吗

相关问题 更多 >

    热门问题