Personal tools
You are here: Home → Comp 171 Scripting Practicum → Course Materials → Python Materials → Python Programs → Creating a .csv file
Document Actions

Creating a .csv file

by Dr. William L. Honig — last modified 2012-02-18 11:24 PM

Use your script to put data into a .csv file that you can open with Excel. Just the basics in this simple example.

Click here to get the file

Size 1 kB - File type text/python-source

File contents

# 
#Comp 171 Standard Python Script Header 
#Assignment name: Write materials to a .csv file the works with Excel 
#LUC id and Name whonig@luc.edu  William Honig 
#Date 19 Feb 2012
# 
#Simple example of script that writes a text file suitable to open in Excel
# 
#Questions: Want to do more?  See the documents in the Standard Library
#           13.1. csv — CSV File Reading and Writing
# 
#
import csv

def writeCsv():
    newCsv = open( "myResults.csv", "a", newline="" )
    csvWriter = csv.writer( newCsv )
    #csvWriter = csv.writer( newCsv, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL )
    csvWriter.writerow( ["one", "two", "three", "two"*3] )
    csvWriter.writerow( ["Next Line", 23*5, 12/5, "here we go "*3] )
    newCsv.close()
« March 2027 »
Su Mo Tu We Th Fr Sa
123456
78910111213
14151617181920
21222324252627
28293031
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: