63067

Trying to add sums from a csv file in python

Question:

I need to add sums of a csv file. The program is a test for a travel reservation system and the file reads like this:

availableSTART,reservations,cancellations,availableEND 20,1,0,18

I need to subtract reservations from the available start integer, then add back cancellations (Which in this case results to 0) and then print the output, which in this case results in 18. So far, I have opened the CSV file and printed what it contains. Here is my code this far. Any help would be appreciated!

import csv with open ('transactions.csv') as csvfile: readCSV = csv.reader(csvfile, delimiter=',') print (readCSV) for row in readCSV: print(row)

Answer1:

One way to read a CSV file is using a <a href="https://docs.python.org/3.6/library/csv.html#csv.DictReader" rel="nofollow">csv.DictReader</a>.

This object can iterate your CSV file and return a dictionary which keys are columns names and values are row values. But you need to convert string values to integers:

>>> import io >>> import csv >>> with io.open('transactions.csv') as csvfile: ... reader = csv.DictReader(csvfile) ... for row in reader: ... values = {k: int(v) for k, v in row.items()} ... print(values) ... {'availableEND': 18, 'reservations': 1, 'availableSTART': 20, 'cancellations': 0}

Then you can make your sums…

>>> with io.open('transactions.csv') as csvfile: ... reader = csv.DictReader(csvfile) ... for row in reader: ... values = {k: int(v) for k, v in row.items()} ... sums = values['availableSTART'] + values['cancellations'] - values['reservations'] ... print(sums) 19

Recommend

  • Check for stdout or stderr
  • PyRun_String stop sending result to stdout after any error
  • GMP Bit shift doesn't work on negative numbers
  • Timer once a minute on the minute
  • Finding the number of elements less than or equal to k in a multiset
  • Sending rails errors to rspec output
  • How to use the resource module to measure the running time of a function?
  • Why doesnt this Java loop in a thread work?
  • Z3: Convert between FP and BitVector?
  • Is playing sound in Javascript performance heavy?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Custom Tabgroup Appcelerator
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Insert into database using onclick function
  • Projection media query: browser support and workarounds?
  • Different response to non-authenticated users and AJAX calls
  • Counter field in MS Access, how to generate?
  • Knitr HTML Loop - Some HTML output, some R output
  • Deserializing XML into class C#
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Convert array of 8 bytes to signed long in C++
  • How to make Safari send if-modified-since header?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • align graphs with different xlab
  • Return words with double consecutive letters
  • How to pass list parameters for each object using Spring MVC?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • python regex in pyparsing
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • JaxB to read class hierarchy
  • Reading document lines to the user (python)
  • How to Embed XSL into XML
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?