Reading and writing in Python

suggest change

CSV(Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet. This is a minimal example of how to write & read data in Python.

Writing data to a CSV file:

import csv
data = [["Ravi", "9", "550"], ["Joe", "8", "500"], ["Brian", "9", "520"]]
with open('students.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerows(data)

Reading data from a CSV file:

import csv
with open('students.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',',)
    for row in spamreader:
        print("Name: {} class: {} Marks: {}".format(row[0], row[1], row[2]))
output:
Name: Ravi class: 9 Marks: 550
Name: Joe class: 8 Marks: 500
Name: Brian class: 9 Marks: 520

Feedback about page:

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


Reading and Writing CSV:
* Reading and writing in Python

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
77 Reading and Writing CSV
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