You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Francis Irving edited this page May 31, 2013
·
1 revision
Comma-separated value files are a quite commonly used text form of
spreadsheet. To process them often requires special cases, such as
parsing dates or removing bad rows.
Download the CSV file first. (If there are quirks in the input file, you
might at this point want to preprocess the data using, for example, the
.replace function)
Load it into the standard Python CSV reader. It needs to be a list of lines.
importcsvreader=csv.reader(data.splitlines())
You can then loop through the rows as if they were a list.
forrowinreader:
print"£%s spent on %s"% (row[7], row[3])
Saving to the datastore
Conventionally the first line gives the names for the columns. You
can get the standard reader to load in each row as a dictionary, where
the keys are those names.
reader=csv.DictReader(data.splitlines())
This makes it easy to save the data. By default everything
comes out as strings. We convert the 'Amount' row to a number type,
so that it can then be added and sorted.