0

I have downloaded the billing reports from AWS, which are in CSV format, onto my server.

Now, I have to parse those CSV files in Python so that it shows consolidated/individual cost information on day/week/monthly basis.

Can anyone please help me with this?

import csv 
with open('588399947422-aws-billing-detailed-line-items-2015-09.c‌​sv') as csvfile: 
    readCSV = csv.reader(csvfile,delimiter=',') 
    for row in readCSV : 
        print row

CSV headers

"InvoiceID","PayerAccountId","LinkedAccountId","RecordType",‌​"RecordId","ProductN‌​ame","RateId","Subsc‌​riptionId","PricingP‌​lanId","UsageType","‌​Operation","Availabi‌​lityZone","ReservedI‌​nstance","ItemDescri‌​ption","UsageStartDa‌​te","UsageEndDate","‌​UsageQuantity","Blen‌​dedRate","BlendedCos‌​t","UnBlendedRate","‌​UnBlendedCost","Reso‌​urceId","user:Applic‌​ation Name","user:Business Unit"
3
  • Please show us the code u have tried Commented Jul 11, 2017 at 9:25
  • Use pandas for csv pandas read csv Commented Jul 11, 2017 at 9:26
  • import csv with open('588399947422-aws-billing-detailed-line-items-2015-09.csv') as csvfile: readCSV = csv.reader(csvfile,delimiter=',') for row in readCSV : print row I don't know how to display cost info on day/week/monthly basis Commented Jul 11, 2017 at 9:40

1 Answer 1

1

Use built-in csv module.

From docs:

>>> import csv
>>> with open(path_to_your_file, 'rb') as csvfile:
...     reader = csv.reader(csvfile, delimiter=',', quotechar='|')
...     for row in reader: # iterate over reader line by line (line is a list of values in this case)
...         print row # list of values

First, you have to open csv, the best option is to use with open(filename,'rb') as f:.

Then, instantiate reader - you have to specify delimiter (comma in most cases) and quotechar (quotes if there are some).

Then you can iterate over reader line by line.

Sign up to request clarification or add additional context in comments.

5 Comments

How is that going to display cost information on day/week/monthly basis?? This code will just read my csv file.
You didn't attached form of your csv, format of your csv, headers etc. How we could know what is cost on daily bases? Rewrite your question.
"InvoiceID","PayerAccountId","LinkedAccountId","RecordType","RecordId","ProductName","RateId","SubscriptionId","PricingPlanId","UsageType","Operation","AvailabilityZone","ReservedInstance","ItemDescription","UsageStartDate","UsageEndDate","UsageQuantity","BlendedRate","BlendedCost","UnBlendedRate","UnBlendedCost","ResourceId","user:Application Name","user:Business Unit"
These are the headers of my csv file. So, I need to parse it so that it'll be able to give me billing info on daily or weekly or monthly basis
@Ashima I think this is more of a data specific question rather than a code specific one. I doubt if we ll be able to do something without knowing the nature of the data as this is more specific to your requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.