Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os,sys
  4.  
  5. if len(sys.argv) < 3:
  6. print "Usage: %s <source file> <percentage>" % sys.argv[0]
  7. exit(1)
  8.  
  9. if not os.path.exists(sys.argv[1]):
  10. print "Provide a file from wich to extract the sample"
  11. exit(1)
  12.  
  13. if not (int(sys.argv[2]) < 100 and int(sys.argv[2]) > 1):
  14. print "Provide a percentage of the sample to take (a number between 1 to 100)"
  15. exit(1)
  16.  
  17. srcfile=sys.argv[1]
  18. percentage=float(sys.argv[2])
  19.  
  20. nlines=0
  21. sf=open(srcfile,"r")
  22. nlines=sum(1 for _ in sf)
  23.  
  24. nresult=int((float(nlines)*(float(percentage)/100.0)))
  25. step=nlines/nresult
  26.  
  27. sf.seek(0)
  28. i=0
  29. for line in sf.readlines():
  30. i+=1
  31. if (i%step) == 0:
  32. print line.strip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement