简体   繁体   中英

How to use python readlines method in random order

How can one use the readlines() method to read a file in a random shuffled manner ie random.shuffle()

file = open(filename)
data = file.readlines()           
file_length =  len(data)

将它们放入带有lines = file.readlines()的列表中,然后列出列出的random.shuffle(lines) (导入random模块)。

You can store the entire file as a list of lines with:

f = open(filename)
data = f.read() # the whole file in one string
lines = data.split('\n')

Then use random to access lines.

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