I got the basics of parsing .csv files and putting certain lines into lists and/or dictionaries but this one I cant crack.
There are 9 lines with general information like
- client name
- invoice number
- invoice date
- ...etc
And then there is detailed listing of product and price. What I wish to do is:
- get 'Invoice #', 'Issue date', 'Due date' and 'Amount due' from the first 9 lines
- Get just the 'Description' and 'Amount' from the remaining lines
into a dictionary. I will then write this data into a mySql database. Can someone suggest how do I start adding items to a dictionary after this "header" (line 9)?
Thanks.
Bill to Client
Billing ID xxxx-xxxx-xxxx
Invoice number 3359680287
Issue date 1/31/2016
Due Date 3/1/2016
Currency EUR
Invoice subtotal 2,762,358.40
VAT (0%) 0
Amount due 2,762,358.40
Account ID Account Order Purchase Order Product Description Quantity Units Amount
xxx-xxx-xxxx Client - Search, GDN, Youtube Client- Google Search Google AdWords Belgium_GDN_january_(FR) 1 Impressions 0.04
xxx-xxx-xxxx Client - Search, GDN, Youtube Client- Google Search Google AdWords UK_GDN_january 392 Impressions 2.92
xxx-xxx-xxxx Client - Search, GDN, Youtube Client- Google Search Google AdWords Poland_GDN_january 12 Impressions 0.05
xxx-xxx-xxxx Client - Search, GDN, Youtube Client Google AdWords Switzerland Family vacation 251 Clicks 4,718.91
xxx-xxx-xxxx Client - Search, GDN, Youtube Client Google
xxx-xxx-xxxx Client - Search, GDN, Youtube Client Google AdWords Invalid activity -16.46
When I try this code:
import csv
with open('test.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=",")
for row in readCSV:
print(row[0])
I get this in terminal:
Bill to
Billing ID
Invoice number
Issue date
Due Date
Currency Invoice
subtotal
VAT (0%)
Amount due
Traceback (most recent call last): File "xlwings_test.py", line 7, in print(row[0]) IndexError: list index out of range xlwings git:master ❯