简体   繁体   中英

Gather a random sample of 5 from text file Python

I want to grab 5 psuedorandom items from a list containing 100,000. These list items are 25 characters long, currently my script is only getting the first characters (and they dont appear to be coming from the list).

My code

import random

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

    print sample_five

UPDATE Throwing error of: object of type 'file' has no len() on line: sample_five = random.sample(hashes, 5)

List in output.txt

['KP9UF8GMKH0VNA1TURAAG6IHO','EQNUB4W1BYM7B5R6IWNWGEV4&','6QQSUUMCW9XPFVMQZUJKMAMW0','AZFUE8GWKHEVNO1TUEAADIHS','0DLWPEOD8345QASDZXCVBNHSD']

in print sample_five I want to output the list as shown above, but its outputting:

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

Can anyone see why it isnt grabbing (5) random list items from the list? Thanks

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM