Advertisement
goebelmasse

Zufällige Stichprobe von Zeilen aus einer Datei ermitteln

May 22nd, 2015
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement