从文本文件Python中收集5个随机样本

时间:2015-06-12 21:05:51

标签: python

我想从包含100,000的列表中获取5个 psuedorandom 项。这些列表项长度为25个字符,目前我的脚本只获取第一个字符(并且它们似乎不是来自列表)。

我的代码

import random

with open("output.txt") as hashes:
    sample_five = random.sample(hashes, 5)

    print sample_five

更新投放错误:object of type 'file' has no len()在线:sample_five = random.sample(hashes, 5)

output.txt

中的列表
['KP9UF8GMKH0VNA1TURAAG6IHO','EQNUB4W1BYM7B5R6IWNWGEV4&','6QQSUUMCW9XPFVMQZUJKMAMW0','AZFUE8GWKHEVNO1TUEAADIHS','0DLWPEOD8345QASDZXCVBNHSD']
print sample_five中的

我想输出如上所示的列表,但是输出:

>>> [34378, 41139, 76739, 27686, 23880]

任何人都可以看到它为什么不从列表中抓取(5)随机列表项?感谢

1 个答案:

答案 0 :(得分:1)

import random
import ast
with open("output.txt") as hashes:
    input = ast.literal_eval(hashes.read())
    sample_five = random.sample(input, 5)

    print sample_five
相关问题