I’ve been studying Python for two weeks and there’s something I want to know when reading and writing CSV file in Python.
I used def codes for output:
def csv(filename):
f = open(filename, 'rU')
lines = []
for line in f:
lines.append(line.rstrip('\n').split(','))
f.close()
return lines
And the result from above codes looks like:
[[‘a’, ”, ”, ”, ”, ”],
[‘b’, ”, ”, ”, ”, ”],
[‘c’, ”, ”, ”, ”, ”],
[‘d’, ”, ”, ”, ”, ”],
[‘e’, ”, ”, ”, ”, ”],
[‘f’, ”, ”, ”, ”, ”],
[‘g’, ”, ”, ”, ”, ”]]
Let’s say I want to change the value of first row that contains 'a'.
What codes can I use to define the row containing a?
First, use built-in csv parser.
This code will define a python list of rows which contains ‘a’: