Python
- 2 ответа
- 0 вопросов
1
Вклад в тег
n,mark,type,view,date
0,bmw,v1,2,01-01-2020
1,bmw,v1,8,02-01-2020
2,bmw,v2,5,04-02-2020
3,audi,v1,3,09-02-2020
4,audi,v1,8,21-03-2020
5,kia,v1,13,24-03-2020
import csv
from datetime import datetime
def get_key(row, basic_path=('mark', 'type',), date_path=('year',)):
key = '_'.join((str(row[bp]) for bp in basic_path))
extra_key = '-'.join((str(getattr(row['date'], dp)) for dp in date_path))
return f'{key}_{extra_key}'
def combine_rows(target_row, row):
target_row['view'] += row['view']
def read_rows(path):
with open(path, 'r') as handler:
reader = csv.DictReader(handler)
for row in reader:
row['view'] = int(row['view'])
row['date'] = datetime.strptime(row['date'], '%d-%m-%Y')
yield row
def main():
result = dict()
basic_path = ('mark', 'type',)
date_path = ('year',)
for row in read_rows('test.csv'):
key = get_key(row=row, basic_path=basic_path, date_path=date_path)
if key not in result:
result[key] = row
else:
combine_rows(target_row=result[key], row=row)
for value in result.values():
print(value)
if __name__ == '__main__':
main()
basic_path = ('mark', 'type',)
date_path = ('year',)
{'n': '0', 'mark': 'bmw', 'type': 'v1', 'view': 10, 'date': datetime.datetime(2020, 1, 1, 0, 0)}
{'n': '2', 'mark': 'bmw', 'type': 'v2', 'view': 5, 'date': datetime.datetime(2020, 2, 4, 0, 0)}
{'n': '3', 'mark': 'audi', 'type': 'v1', 'view': 11, 'date': datetime.datetime(2020, 2, 9, 0, 0)}
{'n': '5', 'mark': 'kia', 'type': 'v1', 'view': 13, 'date': datetime.datetime(2020, 3, 24, 0, 0)}
basic_path = ('mark', 'type',)
date_path = ('month', 'year',)
{'n': '0', 'mark': 'bmw', 'type': 'v1', 'view': 10, 'date': datetime.datetime(2020, 1, 1, 0, 0)}
{'n': '2', 'mark': 'bmw', 'type': 'v2', 'view': 5, 'date': datetime.datetime(2020, 2, 4, 0, 0)}
{'n': '3', 'mark': 'audi', 'type': 'v1', 'view': 3, 'date': datetime.datetime(2020, 2, 9, 0, 0)}
{'n': '4', 'mark': 'audi', 'type': 'v1', 'view': 8, 'date': datetime.datetime(2020, 3, 21, 0, 0)}
{'n': '5', 'mark': 'kia', 'type': 'v1', 'view': 13, 'date': datetime.datetime(2020, 3, 24, 0, 0)}