Python Programming Assistance?

I am having trouble getting the answers on these two exercises. I have coded the setup for google drive but got to a stand still at this part. There is a google drive folder that is full of the required files for this assignment as well. I would appreciate it if someone could take a look at it see what they think. 

Thank you in advance!

Attachment image

1 Answer

Relevance
  • 1 month ago

    That's awfully fuzzy, but the first part seems to be about reading a CSV file. That's just reading a text file and splitting each line at commas to get a list of fields.

    Splitting is easy if you don't need to worry about quotes or escapes that might include a comma as part of a field instead of a field separator.  If the variable line has a line of the file, with any newline ('\n') character removed, then:

        fields = line.split(',')

    ...will give you a list of fields.  No sweat.

    Removing the newline is necessary so that it does get attached to the last field in each line (except for the last line, which may not have a newline).  There can be at most one newline, at the end, so removing is pretty easy:

        line = line.rstrip('\n')

    If you do need to deal with commas, then the problem is that there is more than one method for quoting them.  That is, there is no standard, so different popular applications have come up with different ways to handle embedded commas.  There's a standard module in the Python library (import csv) that exists mostly because of this, and also to not worry about newlines.

        import csv

        filename = input("csv file to read: ")

        with open(filename) as csv_file:

            csv_reader = csv.reader(csv_file)

            csv_rows = list(csv_reader)

        except:

            csv_rows = None

    That converts a whole csv file into a list of records, with each record being a list of fields in one line that were split by commas.  All the default formatting values are taken, which will read anything without quotes, and will also recognize quoting performed by Microsoft Excel.

Still have questions? Get your answers by asking now.