Python - Reading CSV from python
- Due Oct 30, 2016 by 11:59pm
- Points 10
- Submitting a file upload
- File Types py
Questions? Assignment Discussion: Reading and Writing SQL from Python
For Q1 you'll leave csv on the server. (in ~/assign/assign-csv) For Q2 and Q3 provide your answers in a firstname_lastname.py file which is both uploaded and in the ~/assign/assign_csv folder. To help with grading leave all answers uncommented. Separate the output from each sub question with a separator like this (change the number):
print("---------------Q2.1----------------")
1. Using phpmyadmin execute query from Q5, 6, and 7 in Queries 3 homework and export the results as CSVs. (3 points, one per query). Note that I've fixed the export defaults so that columns names are included by default for csvs (you're welcome!).
2. Read those CSV back in and print results out as sentences (using .format() ) that make sense of the results, one per row. That requires reaching into the row dict to identify individual fields. You do not need to transform the results of the query beyond simple formatting, so displaying month number is adequate. (3 points, one per query).
3. (4 points) Read a CSV and output it as a fixed width table; it's ok to output it just to the terminal using print (you don't need to write it into a file from python, but if you like you can save output to the terminal by using the unix redirect ('>') like:
python3 fixedwithoutput.py > output_tile.txt
Each column should be separated by minimum 2 spaces, the column headers should left align with their content and the headers should be divided from the content with a line made of dashes. Hint: this involves looking at maximum length of items in each column and adding spaces after the item to pad out the column. But to find the largest items you have to run through the file once first, then run through a second time to print out the padded data). We will run your code against two different input CSVs (neither you will not see beforehand, one with have 3 columns and the other will have an unknown number of columns).
This picture shows an example (with 3 columns), get this one working first then try others. The input file is available with:
cp /home/jhowison/data_wrangling_class_files/7_csv/foundation.csv ~/
Rubric
Criteria | Ratings | Pts | ||
---|---|---|---|---|
Q1 (one per query)
threshold:
pts
|
|
pts
--
|
||
Q2 (one per query)
threshold:
pts
|
|
pts
--
|
||
Q3-1: Using Dict
threshold:
pts
|
|
pts
--
|
||
Q3-2: Finding the width of columns
threshold:
pts
|
|
pts
--
|
||
Q3-3: Printing out, padding spaces
threshold:
pts
|
|
pts
--
|
||
Q3-4: Printing out heading & dividing
threshold:
pts
|
|
pts
--
|
||
Total Points:
10
out of 10
|