Sunday, April 8, 2018

File Import

As mentioned in the previous post, this post will be about how to import a csv file into the Python environment.

I started by creating an excel file called ClaimsToday.xls containing fictitious information for 6 different claims. In order to transition smoothly into the checks for validity that will be addressed in the next post, I configured the below data such that out of the six claims, only one claim has all valid data fields:



Note the following:

  • Claim 123456 has the email field missing
  • Claim 121212 has the l_name field missing
  • Claim 123123 has f_name and email missing
  • Claim 131313 is the one valid claim with all information correct
  • Claim 1234 has an invalid 4 digit claim number
  • Claim 111007 has a string in the phone field, where there needs to be a number
Given this data, moving forward, the program will need to flag all claims except claim 131313, whose information the program will email. The program will print a message describing the deficiencies in the other claims and will not attempt to email their information to the claimant.

Once I created the excel file, I saved it as ClaimsToday.csv.

Below is the code to import the csv file. We import and access the data using a reader. (If you were in Advance Webdesign last semester, we learned about this in that class, but using C#!) This code recognizes the first row of our data as fieldnames by default. After reading the file, I have a print statement to verify that my code worked the way I wanted it to.



Here's the result of the print statement:


Now that we have the data we need, in my next post I will be moving forward into showing you how we can check the data before sending out the email.


Sources:
https://docs.python.org/3/library/csv.html#csv.DictReader

4 comments:

  1. Great post!! Could you explain cvs and cvs.DictReader a bit more?

    ReplyDelete
    Replies
    1. Csv stands for "Comma Separated Values." It is a basic file type that separates each value in a row of data with a comma, and does so line by line. Saving our excel file as a csv makes the import very easy. We use the reader to read through the data. (We would do the same thing in other languages like C#). The reader allows us to access the data from the file. The reader is csv.DictReader(csvfile) because it reads each row of the csv as a dictionary entry (see previous post on dictionaries) with multiple fields. I hope that answers your question!

      Delete
  2. Does Python use Try...Catch blocks, like we used in C# to throw an exception if the file is null or contains incorrect data? Or is there another way to verify the file has the correct data so you don't get an error?

    ReplyDelete
    Replies
    1. Python does have the capability to use Try/Catch blocks. I didn't think to do that, since my code worked pretty easily without any errors, but this is a good suggestion if I were to refactor my code in the future, to avoid program crashes. That's a really good point!

      This is what the a Try/Catch (Try/Except in Python) would look like:

      try:
      print "Hello World"
      except:
      print "This is an error message!"

      And here's a link with more information:

      http://www.pythonforbeginners.com/error-handling/python-try-and-except

      Delete