Я создал команду в django, которая читает файл csv и загружает данные в базу данных.В настоящее время я могу читать только один файл, как я могу прочитать несколько файлов в каталоге со следующей реализацией ниже.Я думаю, что у меня должен быть какой-то список каталогов, и затем для каждого файла я буду выполнять заполнение.
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
import csv
from django.core.management.base import BaseCommand
from elite_schedule.models import Match
SILENT, NORMAL, VERBOSE, VERY_VERBOSE = 0, 1, 2, 3
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
from glob import iglob
path=os.path.join(BASE_DIR, '/data/')
allFiles = glob.glob(path + "/*.csv")
list_ = []
class Command(BaseCommand):
help = ("Imports movies from a local CSV file. " "Expects title, URL, and release year.")
# def add_arguments(self, parser):
# # Positional arguments
# parser.add_argument("file_path",nargs=1,type=str,)
def handle(self, *args, **options):
verbosity = options.get("verbosity", NORMAL)
file_path = x
print(file_path)
if verbosity >= NORMAL:
self.stdout.write("=== Matches imported ===")
for file_ in allFiles:
with open(file_path) as f:
reader = csv.reader(f)
# Dont upload header froom csv
next(reader)
for game in reader:
division = game[0]
date=game[1]
home_team = game[2]
away_team = game[3]
home_goal = game[4]
away_goal = game[5]
print(home_goal)
home_odd = game[23]
draw_odd = game[24]
away_odd = game[25]
# let's skip the column captions
# continue
"""Assign country based on division.
to get divisions code details check football.uk
"""
try:
if division == "E1" or "E2" or "E3":
country = "ENGLAND"
elif division == "S1":
country = "SPAIN"
elif division == "G1":
country="GERMANY"
elif division == "I1":
country = "ITALY"
match, created = \
Match.objects.get_or_create(
division=division,
date=date,
home_team=home_team,
away_team=away_team,
home_goal =home_goal,
away_goal=away_goal,
home_odd=home_odd,
draw_odd=draw_odd,
away_odd=away_odd,
country=country
)
if verbosity >= NORMAL:
self.stdout.write("{}. {}".format(game, match.division))
except Exception as e:
raise e
Моя папка данных находится на корневом уровне проекта django, а папки csv организованы какследует:
├── ENGLAND
│ ├── championship.csv
│ ├── conference.csv
│ ├── league_1.csv
│ ├── league_2.csv
│ ├── notes.txt
│ └── premier_league.csv
├── GERMANY
│ ├── bundesliga_1.csv
│ └── bundesliga_2.csv
├── ITALY
│ ├── seria_a.csv
│ └── seria_b.csv
└── SPAIN
├── la_liga_primiera_division.csv
└── La_Liga_Segunda Division.csv