Skip to main content

Comma-Separated Values , Make it Beautiful! , Filter the Output



 Comma-Separated Values
CSV files are a way of storing a spreadsheet as a text file. Every value in the file is separated by a comma.
Hence the name...
Look, it's basically a spreadsheet.
Opening A CSV File
Fortunately, CSV files are so common that Python already has built-in libraries for working with them.
👉 The csv file 'January.csv' has been added for you. Let's see what happens:
import csv # Imports the csv library
with open("January.csv") as file: # Opens the csv file
  reader = csv.reader(file) # reads the contents of the csv file into the 'reader' variable
  line = 0
  for row in reader: # loop to output each row in the 'reader' variable one at a time.
    print (row)

This works, but the output isn't very pretty. And we like pretty.

Make it Beautiful!

👉 Let's use join. It allows us combine lists in a more interesting way.

import csv 
with open("January.csv") as file: 
  reader = csv.reader(file) 
  line = 0
  for row in reader: 
    print (", ".join(row)) # adds a comma and space and then joins data, you could try joining with tabs too with `\t`


Filter the Output

👉 The trick is to treat the CSV like a dictionary, using the csv.DictReader() function. In the code below, I've filtered it so that it only shows the net total from each day.

import csv # Imports the csv library
with open("January.csv") as file: 
  reader = csv.DictReader(file) # Treats the file as a dictionary 
  line = 0
  for row in reader: 
    print (row["Net Total"])

Now let's see if we can add the net totals from each day to create a total. Note that I've cast the data as a float because our library will treat it as text.

import csv # Imports the csv library
with open("January.csv") as file: 
  reader = csv.DictReader(file) # Treats the file as a dictionary 
  total = 0
  for row in reader: 
    print (row["Net Total"])
    total += float(row["Net Total"]) # Keeps a running total
print(f"Total: {total}") #Outputs



Comments

Popular posts from this blog

WHAT IS Nesting

 WHAT IS Nesting  Nesting is where we put an  if  statement within an  if  statement using the power of indenting. The second  if  statement within the first  if  statement must be indented and its  print  statement needs to be indented one more time.  EXAMPLE:

IF AND ELSE STATEMENT

  If Statements    These statements are a bit like asking a question. You are telling the computer:  if  something is true,  then  do this specific block of code. Double equals ( == ) is asking the computer to compare if these two things are  exactly  the same.  EXAMPLE :  myName = input("What's your name?: ") if myName == "David":  What is else? IF  the condition is  not  met with the  if  statement, then we want the computer to do the  else  part instead. Likewise, if the condition  is  met in the  if  statement, then the  else  bit is ignored by the computer. The  else  statement must be the first thing  unindented  after the  if  statement and in line with it    EXAMPLE :  myName = input("What's your name?: ") if myName == "David":  print("Welcome Dude!")  print("You're just the baldest dude I've ever seen...

FOR LOOP , RANGE

  FOR LOOP  A  while  loop is perfect to use when we  don't  know how many times we want the loop to repeat.  If we have an idea of how many times we want the loop to repeat, we can use a  for  loop to loop code in exactly the same way the  while  loop did.  EXAMPLE :  for counter in range(10):   print(counter) RANGE  The  range  function creates a list of numbers in the range you create. If you only give it one number, it will start at  0  and move to a state where the final number is  one less  than the number in the brackets. In this case, the final number would be  9 .  EXAMPLE :  total = 0 for number in range(100) :   total += number   print(total)