Basic Write Example

suggest change
import csv

#------ We will write to CSV in this function ------------

def csv_writer(data, path):
    
    #Open CSV file whose path we passed.
    with open(path, "wb") as csv_file:
        
        writer = csv.writer(csv_file, delimiter=',')
        for line in data:
            writer.writerow(line)

#---- Define our list here, and call function ------------

if __name__ == "__main__":

    """
    data = our list that we want to write. 
    Split it so we get a list of lists.
    """
    data = ["first_name,last_name,age".split(","),
            "John,Doe,22".split(","),
            "Jane,Doe,31".split(","),
            "Jack,Reacher,27".split(",")
            ]

    # Path to CSV file we want to write to.
    path = "output.csv"
    csv_writer(data, path)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Writing to CSV from String or List:
* Basic Write Example

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve
188 Writing to CSV from String or List