Graphics Programs Reference
In-Depth Information
Put Logic in the Loop
If you look at the code to convert your CSV file to JSON, you should notice
the if-else statement in the for loop, after the three print lines. This checks
if the current iteration is the last row of data. If it isn't, don't put a comma
at the end of the observation. Otherwise, you do. This is part of the JSON
specification. You can do more here.
You can check if the max temperature is more than a certain amount and
create a new field that is 1 if a day is more than the threshold, or 0 if it is
not. You can create categories or flag days with missing values.
Actually, it doesn't have to be just a check for a threshold. You can calcu-
late a moving average or the difference between the current day and the
previous. There are lots of things you can do within the loop to augment
the raw data. everything isn't covered here because you can do anything
from trivial changes to advanced analyses, but now look at a simple
example.
Going back to your original CSV file, wunder-data.txt , create a third column
that indicates whether a day's maximum temperature was at or below
freezing. A 0 indicates above freezing, and 1 indicates at or below freezing.
import csv
reader = csv.reader(open('wunder-data.txt', 'r'), delimiter=”,”)
for row in reader:
if int(row[1]) <= 32:
is_freezing = '1'
else:
is_freezing = '0'
print row[0] + “,” + row[1] + “,” + is_freezing
Like before, read the data from the CSV file into Python, and then iterate
over each row. Check each day and flag accordingly.
This is of course a simple example, but it should be easy to see how you
can expand on this logic to format or augment your data to your liking.
Remember the three steps of load, loop, and process, and expand from
there.
Search WWH ::




Custom Search