Advertisement
daniilak

parser.py

Jul 11th, 2021
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1. import csv
  2.  
  3. from models import *
  4.  
  5.  
  6. import os
  7.  
  8.  
  9. def toFloat(s):
  10.     s = s.replace(',', '.')
  11.     try:
  12.         return float(s)
  13.     except:
  14.         if s == '0,00' or s == '0.00':
  15.             return 0.00
  16.         if s == '':
  17.             return None
  18.         return s
  19.  
  20. def toInt(s):
  21.     s = s.replace(',', '.')
  22.     try:
  23.         return int(s)
  24.     except:
  25.         if s == '0.00':
  26.             return 0
  27.         if s == '':
  28.             return None
  29.         return s
  30.  
  31. def toSmallInt(s):
  32.     s = s.replace(',', '.')
  33.     try:
  34.         s = int(s)
  35.         if s > 32767:
  36.             return 32767
  37.         return s
  38.     except:
  39.         if s == '0.00':
  40.             return 0
  41.         if s == '':
  42.             return None
  43.         return s
  44.  
  45.  
  46. for dirname, _, filenames in os.walk('files'):
  47.     for filename in filenames:
  48.         data = []
  49.         file = os.path.join(dirname, filename)
  50.         print(file)
  51.         with open(file, "r") as f:
  52.             reader = csv.reader(f, delimiter=";")
  53.             index = 0
  54.             for i, line in enumerate(reader):
  55.                 index = index + 1
  56.                 if index == 1:
  57.                     continue
  58.                 dictAppend = {
  59.                     'id_ref': toInt(line[0]),
  60.                     'region_id': line[1],
  61.                     'area_id': line[2],
  62.                     'city_id': line[3],
  63.                     'street_id': line[4],
  64.                     'houseguid': line[18],
  65.                     'management_organization_id': toInt(line[19]),
  66.                     'built_year': toSmallInt(line[20]),
  67.                     'exploitation_start_year': toSmallInt(line[21]),
  68.                     'project_type': line[22],
  69.                     'house_type': line[23],
  70.                     'is_alarm': line[24],
  71.                     'method_of_forming_overhaul_fund': line[25],
  72.                     'floor_count_max': toSmallInt(line[26]),
  73.                     'floor_count_min': toSmallInt(line[27]),
  74.                     'entrance_count': toSmallInt(line[28]),
  75.                     'elevators_count': toSmallInt(line[29]),
  76.                     'energy_efficiency': line[30],
  77.                     'quarters_count': toSmallInt(line[31]),
  78.                     'living_quarters_count': toSmallInt(line[32]),
  79.                     'unliving_quarters_count': toSmallInt(line[33]),
  80.                     'area_total': toFloat(line[34]),
  81.                     'area_residential': toFloat(line[35]),
  82.                     'area_non_residential': toFloat(line[36]),
  83.                     'area_common_property': toFloat(line[37]),
  84.                     'area_land': toFloat(line[38]),
  85.                     'parking_square': toFloat(line[39]),
  86.                     'playground': toSmallInt(line[40]),
  87.                     'sportsground': toSmallInt(line[41]),
  88.                     'other_beautification': line[42],
  89.                     'foundation_type': line[43],
  90.                     'floor_type': line[44],
  91.                     'wall_material': line[45],
  92.                     'basement_area': toFloat(line[46]),
  93.                     'chute_type': line[47],
  94.                     'chute_count':  toInt(line[48]),
  95.                     'electrical_type': line[49],
  96.                     'electrical_entries_count':  toSmallInt(line[50]),
  97.                     'heating_type': line[51],
  98.                     'hot_water_type': line[52],
  99.                     'cold_water_type': line[53],
  100.                     'sewerage_type': line[54],
  101.                     'sewerage_cesspools_volume': toFloat(line[55]),
  102.                     'gas_type': line[56],
  103.                     'ventilation_type': line[57],
  104.                     'firefighting_type': line[58],
  105.                     'drainage_type': line[59],
  106.                 }
  107.                 data.append(dictAppend)
  108.         try:
  109.             ReformaOpendata.insert_many(data).execute()
  110.         except BaseException as e:
  111.             print(str(e))
  112.             exit()
  113.         print("OK file=", file)
  114. print("OK ALL")
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement