DevOps Engineer. Solutions Architect

Blog 8 Spring 2021: Python Script to Generate CSV Rows

It’s a common task for database administrators to go through a giant list of data with thousands of entries. Normally these entries aren’t structured in any format so passing them into a database takes a bit of formatting. The common data format would be a CSV (comma separated values) which can be imported into a database or Excel spreadsheet. There’s many ways to go about converting lists of data into CSV’s but this guide will cover 2 methods.

Method 1: For loop

  1. Create a new python script and import any necessary modules
    #!/usr/local/bin/python3
    import sys
    import os
    
  2. Depending how you call the list of data entries, create a function that iterates through all the entries and outputs a csv formatted row.
    csv = [data1, data2, data3, data4, data5]
    csv_row = ""
    for item in csv:
    if type(item) = str:
       csv_row += "\"" + item + "\", "
    else:
       csv_row += str(item) + ","
    print(csv_row)
    
  3. Data should be outputted like so:
    "data1","data2","data3","data4","data5"
    

Method 2: Import the built-in Python CSV module

  1. Import python csv module
    #!/usr/local/bin/python3
    import sys
    import os
    import csv
    
  2. Create a function that creates a csv file with the data imported. Here is an example
    with open('eggs.csv', 'w', newline='') as csvfile:
     spamwriter = csv.writer(csvfile, delimiter=' ',
                             quotechar='|', quoting=csv.QUOTE_MINIMAL)
     spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
     spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])