SHARE
TWEET

Zufällige Stichprobe von Zeilen aus einer Datei ermitteln

goebelmasse May 22nd, 2015 263 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # $Id: pickrandom.py,v 1.2 2012/11/15 21:50:17 elias Exp $
  3.  
  4. import sys
  5. import fileinput
  6. import random
  7.  
  8. if len(sys.argv) < 2:
  9.     sys.stderr.write('Benutzung: %s [stichprobe] [dateinamen]' % (sys.argv[0],))
  10.     sys.exit(1)
  11.  
  12. files = sys.argv[2:]
  13. try:
  14.     picklines = int(sys.argv[1])
  15. except ValueError:
  16.     sys.stderr.write('Erster Parameter muss eine Zahl sein\n')
  17.     sys.exit(1)
  18.  
  19. infiles = fileinput.input(files)
  20. lines = [i for i in infiles]
  21. for line in random.sample(lines, picklines):
  22.     sys.stdout.write(line)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top