gpt4 book ai didi

python - 生成 200,000 个哈希的列表并保存到文件

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:08 24 4
gpt4 key购买 nike

我编写了这个快速脚本来生成 200,000 个哈希随机值并将它们保存到一个文件中,但是当我运行该文件时出现此错误:“TypeError: expected a character buffer object "在线:file_.write(hash)

import random

hash = random.SystemRandom()
file_ = open('output.txt', 'w')

count = 0

while count < 200000:
file_.write(hash)

file_.close()

我做错了什么?谢谢。

最佳答案

发现你做错了什么:

打开交互式 shell,键入您的脚本:

>>> import random
>>>
>>> random.SystemRandom()
<random.SystemRandom object at 0x03FD83C8>

这不是数字,也不是文本。您不能将其写入文件。

查看文档:

https://docs.python.org/2/library/random.html#random.SystemRandom

这是一个随机数生成器。您需要像 random 一样使用它。 random.random()random.randint(...) 之类的。

然后在写入文件之前使用 str(...) 将它们转换为文本。

关于python - 生成 200,000 个哈希的列表并保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30808296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com