Function to write lines in CSV file? Or parse list?
Question: I have a python script with no functions. At the moment, it takes in an input
`file1.txt` and outputs a string with results from the calculation, i.e.

    
    
    file1.txt 12 16.0 True
    

I would like to run through dozens of text files and write these strings into
a CSV file, row by row.

Does one append each output into a list, and then parse the list? This feels
like you should write a function to take each input and output the
calculations directly into a CSV file.

How do I take the inputs from each time I run the script, e.g. filename, data
result, data result 2, Boolean result, and write this to the file using
`csv.writer.writerow()`?

    
    
    import csv
    import sys
    
    f = open(sys.argv[1], 'wt')
    try:
        writer = csv.writer(f)
        writer.writerow( ('Filename', 'data', 'data', 'Boolean' )
        writer.writerow( % (f, ..., ..., str(...) )
    finally:
        f.close()
    
    print open(sys.argv[1], 'rt').read()
    


Answer: you can obtain the list of your arguments from sys.argv

    
    
    arg_list = sys.argv[1:]
    

and then create a loop after create the writer

    
    
      f = open(sys.argv[1], 'wt') arg_list = sys.argv[1:] 
      try:
            writer = csv.writer(f)
            writer.writerow( ('Filename', 'data', 'data', 'Boolean' )
            for arg in arg_list:
              writer.writerow( % (f, ..., ..., str(...) ) # your row
      finally:
            f.close()
    
      print open(sys.argv[1], 'rt').read()
    

meanwhile, if you need a single row to write in csv, the code'll be

    
    
    f = open(sys.argv[1], 'wt')
    try:
        writer = csv.writer(f)
        writer.writerow( ('Filename', 'data', 'data', 'Boolean' )
        writer.writerow(sys.argv[2], sys.argv[3], sys.argv[4])
    finally:
        f.close()
    
    print open(sys.argv[1], 'rt').read()
    

