Commit bfddc056 by Patryk Czarnik

zmiany w zapisywaniu i stałe na poziomie klasy

parent 0b8fe26e
class Employee: class Employee:
nazwy_kolumn = ('employee_id', 'first_name', 'last_name', 'job_title', 'salary', 'hire_date',
'department_name', 'address', 'postal_code', 'city', 'country')
SEP = ';'
def __init__(self, employee_id, first_name, last_name, job_title, salary, hire_date, department_name, address, postal_code, city, country): def __init__(self, employee_id, first_name, last_name, job_title, salary, hire_date, department_name, address, postal_code, city, country):
self.employee_id = int(employee_id) self.employee_id = int(employee_id)
self.first_name = first_name self.first_name = first_name
...@@ -20,13 +25,11 @@ class Employee: ...@@ -20,13 +25,11 @@ class Employee:
def read_csv(file_path='emps.csv'): def read_csv(file_path='emps.csv'):
with open(file_path, mode='r', encoding='utf-8') as file: with open(file_path, mode='r', encoding='utf-8') as file:
file.readline() file.readline()
return [Employee(*(line.strip().split(';'))) for line in file] return [Employee(*(line.strip().split(Employee.SEP))) for line in file]
def write_csv(emps, sciezka): def write_csv(emps, sciezka):
with open(sciezka, mode='w', encoding='utf-8') as plik: with open(sciezka, mode='w', encoding='utf-8') as file:
plik.write('employee_id;first_name;last_name;job_title;salary;hire_date;department_name;address;postal_code;city;country\n') print(*Employee.nazwy_kolumn, sep=Employee.SEP, file=file)
for emp in emps: for emp in emps:
print(emp.employee_id, emp.first_name, emp.last_name, emp.job_title, emp.salary, print(*emp.__dict__.values(), sep=Employee.SEP, file=file)
emp.hire_date, emp.department_name, emp.address, emp.postal_code, emp.city, emp.country,
sep=';', file=plik)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment