Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- class CsvReader(object):
- def __init__(
- self,
- csv_file=None):
- self.csv_file = str(csv_file) if csv_file else None
- self.dict_list = []
- self.read_file()
- def read_file(self):
- with open(self.csv_file, newline='') as csvfile:
- reader = csv.DictReader(csvfile, delimiter=',')
- for row in reader:
- self.dict_list.append(row)
- def list_incomes(self):
- filtered = []
- for row in self.dict_list:
- temp_dict = {}
- temp_dict['dato'] = row['Date Acquired']
- temp_dict['verdi'] = row['Cost Basis']
- temp_dict['comment'] = 'Inntekt: ' + row['Volume'] + ' ' + row['Symbol']
- filtered.append(temp_dict)
- return filtered
- # do something with the row
- #print(row['Date Acquired'], int(float(row['Cost Basis']) * 100), row['Currency'], comment)
- def list_sales(self):
- date = None
- sale_sum = float(0)
- filtered = []
- for row in self.dict_list:
- if row['Date Sold']:
- if date == row['Date Sold']: # If the same date, add value to sum
- sale_sum += float(row['Gain'])
- else: # if a NEW UNIQUE non-empty date is encountered, add the current sum to list and start a new line
- if date != None:
- filtered.append({'dato': date, 'gain': sale_sum})
- date = row['Date Sold']
- sale_sum = float(row['Gain'])
- if sale_sum != float(0): # finally, add the sum as the last item in the list
- filtered.append({'dato': date, 'gain': sale_sum})
- return filtered
- reader = CsvReader('2018_gains.csv')
- print(reader.list_incomes())
- print(reader.list_sales())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement