def handle(self, *args, **options): file = 'moscow_bus_stations.csv' with open(os.path.join(settings.BASE_DIR, file), 'r', encoding='cp1251') as csvfile: reader = csv.reader(csvfile, delimiter=';') next(reader) # count = 0 for line in reader: # count += 1 station = Station() station.name = line[1] station.longitude = float(line[2]) station.latitude = float(line[3]) station.save() for route in line[7].split('; '): try: new_route = Route.objects.get(name=route) except Route.DoesNotExist: new_route = Route.objects.create(name=route) station.routes.add(new_route) print(f'доабвлена станция {station.name}') print( f'latitude {station.latitude} : longitude {station.longitude}' ) print(f'Всего доабавлено станций: {Station.objects.count()}') print(f'Всего добоавлено маршрутов: {Route.objects.count()}')
def handle(self, *args, **options): with open('moscow_bus_stations.csv', newline='', encoding='cp1251') as csvfile: reader = csv.DictReader(csvfile, delimiter=';') for row in reader: station = Station() station.id = row['ID'] station.name = row['Name'] station.latitude = row['Latitude_WGS84'] station.longitude = row['Longitude_WGS84'] station.save() for route in row['RouteNumbers'].split('; '): obj, created = Route.objects.get_or_create(name=route) station.routes.add(obj) print('All done')
def handle(self, *args, **options): with open('moscow_bus_stations.csv', 'r') as csvfile: csv_reader = csv.DictReader(csvfile, delimiter=';') for line in csv_reader: station = Station() station.latitude = line["Latitude_WGS84"] station.longitude = line["Longitude_WGS84"] station.name = line["Name"] string = line["RouteNumbers"].split(";") station.save() for i in range(len(string)): route = Route() route.name = string[i].replace(" ", "") route.save() station.routes.add(route) station.save() del string
def handle(self, *args, **options): with open('moscow_bus_stations.csv', newline='', encoding='cp1251') as csvfile: stations_reader = csv.DictReader(csvfile, delimiter=';') for row in tqdm(stations_reader): station = Station() station.name = row['Name'] station.longitude = row['Longitude_WGS84'] station.latitude = row['Latitude_WGS84'] station.save() routes = [route for route in row['RouteNumbers'].split('; ')] for route in routes: object = Route.objects.filter(name=route).first() if object: station.routes.add(object) else: route = Route(name=route) route.save() station.routes.add(route) station.save()
import csv import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") django.setup() from app.models import Route, Station with open('moscow_bus_stations.csv', newline='', encoding='cp1251') as csvfile: reader = csv.DictReader(csvfile, delimiter=';') for row in reader: station = Station() station.id = row['ID'] station.name = row['Name'] station.latitude = row['Latitude_WGS84'] station.longitude = row['Longitude_WGS84'] station.save() for route in row['RouteNumbers'].split('; '): obj, created = Route.objects.get_or_create(name=route) station.routes.add(obj) print('All done')
def loadSTATION(): """Load Stations.""" url = "https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=stations&requestType=retrieve&format=xml" content = urllib.request.urlopen(url).read() root = ET.fromstring(content) for station in root.iter('Station'): if station.find('station_id') is None: print("Skipping") continue stationId = station.find('station_id').text latitude = station.find('latitude').text longitude = station.find('longitude').text elevation = station.find('elevation_m').text site = station.find('site').text if station.find('wmo_id') is not None: wmo_id = station.find('wmo_id').text else: wmo_id = None if station.find('state') is not None: state = station.find('state').text else: state = None country = station.find('country').text if station.find('site_type') is None: metar = None rawinsonde = None taf = None nexrad = None wind_profiler = None wfo_office = None synops = None else: for site_type in station.iter('site_type'): METAR = site_type.find('METAR') TAF = site_type.find('TAF') raw = site_type.find('rawinsonde') NEXRAD = site_type.find('NEXRAD') wind = site_type.find('wind_profiler') wfo = site_type.find('WFO_office') syn = site_type.find('SYNOPS') if METAR is not None: METAR = True else: METAR = False if TAF is not None: TAF = True else: TAF = False if raw is not None: raw = True else: raw = False if NEXRAD is not None: NEXRAD = True else: NEXRAD = False if wind is not None: wind = True else: wind = False if wfo is not None: wfo = True else: wfo = False if syn is not None: syn = True else: syn = False stationDB = Station.query.filter_by(station_id=stationId).first() if stationDB is None: stationDB = Station(station_id=stationId, wmo_id=wmo_id, latitude=latitude, longitude=longitude, elevation_m=elevation, site=site, state=state, country=country, metar=METAR, taf=TAF, rawinsonde=raw, nexrad=NEXRAD, wind_profiler=wind, wfo_office=wfo, synops=syn) db.session.add(stationDB) db.session.commit() elif stationDB.station_id==stationId and (stationDB.wmo_id!=wmo_id or stationDB.latitude!=latitude or stationDB.longitude!=longitude or stationDB.elevation_m!=elevation or stationDB.site!=site or stationDB.state!=state or stationDB.country!=country or stationDB.metar!=METAR or stationDB.taf!=TAF or stationDB.rawinsonde!=raw or stationDB.nexrad!=NEXRAD or stationDB.wind_profiler!=wind or stationDB.wfo_office!=wfo or stationDB.synops!=syn): print(stationDB.station_id) stationDB.wmo_id = wmo_id stationDB.latitude = latitude stationDB.longitude = longitude stationDB.elevation_m = elevation stationDB.site = site stationDB.state = state stationDB.country = country stationDB.metar = METAR stationDB.taf = TAF stationDB.synops = syn stationDB.wfo_office = wfo stationDB.wind_profiler = wind stationDB.nexrad = NEXRAD stationDB.rawinsonde = raw db.session.commit()