Advertisement
mikhailemv

Untitled

Oct 6th, 2022
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1. import csv
  2. import math
  3. import re
  4. from prettytable import PrettyTable
  5.  
  6.  
  7. def formatter(row: dict):
  8.     dict_format = {
  9.         "experience_id": {
  10.             "noExperience": "Нет опыта",
  11.             "between1And3": "От 1 года до 3 лет",
  12.             "between3And6": "От 3 до 6 лет",
  13.             "moreThan6": "Более 6 лет"},
  14.         "premium": {
  15.             'True': 'Да',
  16.             'False': 'Нет'},
  17.         "salary_gross": {
  18.             'False': 'С вычетом налогов',
  19.             'True': 'Без вычета налогов'},
  20.         "salary_currency": {
  21.             "AZN": "Манаты",
  22.             "BYR": "Белорусские рубли",
  23.             "EUR": "Евро",
  24.             "GEL": "Грузинский лари",
  25.             "KGS": "Киргизский сом",
  26.             "KZT": "Тенге",
  27.             "RUR": "Рубли",
  28.             "UAH": "Гривны",
  29.             "USD": "Доллары",
  30.             "UZS": "Узбекский сум"}
  31.     }
  32.     row['salary_all'] = ''
  33.     for key, value in row.items():
  34.         if key == 'key_skills':
  35.             row[key] = ", ".join(value)
  36.         if key == 'experience_id':
  37.             row[key] = dict_format[key][value]
  38.         if key == 'premium':
  39.             row[key] = dict_format[key][value]
  40.         if key == 'salary_from':
  41.             row[key] = '{:,}'.format(math.floor(float(row["salary_from"]))).replace(',', ' ')
  42.         if key == 'salary_to':
  43.             row[key] = '{:,}'.format(math.floor(float(row["salary_to"]))).replace(',', ' ')
  44.         if key == 'salary_gross':
  45.             row[key] = dict_format[key][value]
  46.         if key == 'salary_currency':
  47.             row['salary_all'] = f'{row["salary_from"]} - {row["salary_to"]} ' \
  48.                                 f'({dict_format[key][value]}) ({row["salary_gross"]})'
  49.         if key == 'published_at':
  50.             row[key] = '.'.join(reversed(value[0:10].split('-')))
  51.  
  52.     return row
  53.  
  54.  
  55. def csv_reader(csv_name: str):
  56.     with open(csv_name, encoding='utf-8-sig') as file:
  57.         file_reader = csv.reader(file, delimiter=',')
  58.         headlines = next(file_reader)
  59.         raw_list = list(file_reader)
  60.         # return [raw_list, headlines]
  61.         csv_filer(raw_list=raw_list,
  62.                   headlines=headlines)
  63.  
  64.  
  65. def csv_filer(raw_list: list, headlines: list):
  66.     vacancies_list = list()
  67.     index_key_skills = headlines.index('key_skills')
  68.     html_tags = re.compile('<.*?>')
  69.     for line in raw_list:
  70.         try:
  71.             line.remove('')
  72.         except ValueError:
  73.             pass
  74.         for index in range(len(line)):
  75.             if index != index_key_skills:
  76.                 line[index] = ' '.join(re.sub(html_tags, '', line[index])
  77.                                    .strip().split())
  78.             else:
  79.                 line[index] = list(map(lambda x: x.strip(), line[index].split('\n')))
  80.  
  81.         if len(line) == len(headlines):
  82.             vacancies_list.append({headlines[i]: line[i] for i in range(len(headlines))})
  83.  
  84.     # return vacancies_list
  85.     print_vacancies(vacancies_list=vacancies_list)
  86.  
  87.  
  88. def print_vacancies(vacancies_list: list):
  89.     for vacancy in vacancies_list:
  90.         current_vacancy = formatter(vacancy)
  91.         print(f'Название: {current_vacancy["name"]}\n'
  92.               f'Описание: {current_vacancy["description"]}\n'
  93.               f'Навыки: {current_vacancy["key_skills"]}\n'
  94.               f'Опыт работы: {current_vacancy["experience_id"]}\n'
  95.               f'Премиум-вакансия: {current_vacancy["premium"]}\n'
  96.               f'Компания: {current_vacancy["employer_name"]}\n'
  97.               f'Оклад: {current_vacancy["salary_all"]}\n'
  98.               f'Название региона: {current_vacancy["area_name"]}\n'
  99.               f'Дата публикации вакансии: {current_vacancy["published_at"]}\n')
  100.  
  101.  
  102. csv_file = 'vacancies_medium.csv'
  103. csv_reader(csv_file)
  104. table = PrettyTable()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement