title: Best way to get a biased selection from a file using python
body: <p>Is there a better way to write this code? It reads a text file (specified when called) the first line contains weighting factors for the file. Weighting factors apply to the first lines of the file and are always less than or equal to the number of items in the file, a weight of 1 is assigned to any other items. It then chooses one or more lines from the file.</p>
  
  <pre><code>#!/usr/bin/env python3
  import random
  from sys import argv
  
  action = argv[1]
  repetition = int(argv[2])
  
  def fileopen(a,b):                              
      with open(a,"r") as f:
          b = [line.rstrip("\n") for line in f]
      return b
  
  #strips new lines, returns list
  
  action = fileopen(action+".txt",action) 
  
  #passes argv[1] to fileopen        
  
  weight = action.pop(0)                          
  weight = (weight.split(","))                    
  for j in range(len(weight)):
      weight[j] = int(weight[j])                  
  for y in range(len(action)-len(weight)):        
      weight.append(1)                            
  
  #assigns weighting to a list and pads it with 1s if needed.
  
  for k in range(repetition):                     
      print(random.choices(action,weight)) 
  
  
  </code></pre>