繁体   English   中英

从Python文件中读取大量随机行

[英]Read a number of random lines from a file in Python

有人可以告诉我如何从Python文件中读取随机的行数吗?

您的要求有点含糊,因此这是另一种略有不同的方法(如果没有其他帮助,则可以作为启发):

from random import random
lines = [line for line in open("/some/file") if random() >= .5]

与其他解决方案相比,行变化较小(分布在总行数的一半左右),但是每行的选择概率为50%,并且只需要遍历文件一次。

要从文件中随机获取许多行,可以执行以下操作:

import random
with open('file.txt') as f:
    lines = random.sample(f.readlines(),5)

上面的示例返回5行,但是您可以轻松地将其更改为所需的数字。 您还可以将其更改为randint()以获取除随机行数之外的随机行数,但是您必须确保样本大小不大于文件中的行数。 根据您的输入,这可能是微不足道的或更复杂的。

需要注意的是该行可能出现在lines中它们出现在文件中不同的顺序。

import linecache
import random
import sys


# number of line to get.
NUM_LINES_GET = 5

# Get number of line in the file.
with open('file_name') as f:
    number_of_lines = len(f.readlines())

if NUM_LINES_GET > number_of_lines:
     print "are you crazy !!!!"
     sys.exit(1)

# Choose a random number of a line from the file.
for i in random.sample(range(1,  number_of_lines+1), NUM_LINES_GET)
    print linecache.getline('file_name', i)

linecache.clearcache()
import os,random

def getrandfromMem(filename) :
  fd = file(filename,'rb')
  l = fd.readlines()
  pos = random.randint(0,len(l))
  fd.close()
  return (pos,l[pos])

def getrandomline2(filename) :
  filesize = os.stat(filename)[6]
  if filesize < 4096 :  # Seek may not be very useful
    return getrandfromMem(filename)

  fd = file(filename,'rb')
  for _ in range(10) : # Try 10 times
    pos = random.randint(0,filesize)
    fd.seek(pos)
    fd.readline()  # Read and ignore
    line = fd.readline()
    if line != '' :
       break

  if line != '' :
    return (pos,line)
  else :
    getrandfromMem(filename)

getrandomline2("shaks12.txt")

假设偏移量始终位于文件的开头:

import random
lines = file('/your/file').read().splitlines()
n_lines = random.randrange(len(lines))
random_lines = lines[:n_lines]

请注意,这会将整个文件读入内存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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