Great job! In the previous two exercises, we only used entire rows (i.e., we didn't access any specific fields within those rows).
Now, let's analyze how we can pick the information we want from rows. In the example below, we only print information on departments whose budgets exceed $350,000.00:
with open('departments.csv') as csv_file:
csv_reader = csv.reader(csv_file)
line_count = 0
for row in csv_reader:
if line_count > 0 and float(row) > 350000.0:
print('The', row, 'department has a budget of',row, 'USD.')
line_count += 1
As we've seen before, each row in our CSV file is essentially a list of strings. This means we can access its elements using square brackets:
row is the first field,
row is the second field, etc. We know the structure of our CSV file, and we know that
row (the third field) is the department's budget. That's why we wrote the condition
float(row) > 350000.0. Remember that the CSV parser treats all fields as strings, so we have to convert
row to float.