示例#1
0
    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()}')
示例#2
0
 def station_save_to_db(item, station):
     # Если записи в БД нет - создаем запись
     item = Station(latitude=station['latitude'],
                    longitude=station['longitude'],
                    name=station['name'], )
     item.save()
     # Делаем привязку к routes
     for route in station['routes']:
         item.routes.add(Route.objects.filter(name=route).values('id')[0]['id'])
         item.save()
示例#3
0
 def handle(self, *args, **options):
     with open(CSV_FILENAME, 'rt') as csv_file:
         csv_reader = csv.DictReader(csv_file, delimiter=';')
         for row in csv_reader:
             print(row['Latitude_WGS84'], row['Longitude_WGS84'], row['Name'])
             print(row['RouteNumbers'].split(';'))
             station = Station(
                 latitude=row['Latitude_WGS84'],
                 longitude=row['Longitude_WGS84'],
                 name=row['Name'],
             )
             station.save()
             for route_name in row['RouteNumbers'].split(';'):
                 route, created = Route.objects.get_or_create(name=route_name)
                 station.routes.add(route)
示例#4
0
    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')
示例#5
0
    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
示例#6
0
    def handle(self, *args, **options):
        with open(CSV_FILE, 'r', encoding=ENCODING) as csv_file:
            reader = csv.DictReader(csv_file, delimiter=';')

            for line in reader:
                station_value = Station(
                    latitude=line['Latitude_WGS84'],
                    longitude=line['Longitude_WGS84'],
                    name=line['Name'],
                )
                station_value.save()

                route_values = [
                    i.strip() for i in line['RouteNumbers'].split(';')
                ]

                for value in route_values:
                    route = Route.objects.get_or_create(name=value)
                    station_value.routes.add(route[0])
                    station_value.save()
    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()
示例#8
0
    def handle(self, *args, **options):
        with open('moscow_bus_stations.csv', 'r') as csvfile:

            reader = csv.DictReader(csvfile, delimiter=';')

            for line in reader:
                station = Station(
                    latitude=line['Latitude_WGS84'],
                    longitude=line['Longitude_WGS84'],
                    name=line['Name']
                )

                station.save()

                route_numbers = [i.strip() for i in line['RouteNumbers'].split(';')]

                for route_number in route_numbers:
                    route = Route.objects.get_or_create(
                        name=route_number
                    )

                    station.routes.add(route[0])
                    station.save()
示例#9
0
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')