Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在django中读取多个文件_Django_Python 3.x_Csv_Django Rest Framework - Fatal编程技术网

如何在django中读取多个文件

如何在django中读取多个文件,django,python-3.x,csv,django-rest-framework,Django,Python 3.x,Csv,Django Rest Framework,我在django中创建了一个命令,用于读取csv文件并将数据种子放入数据库。目前,我只能读取一个文件,如何使用下面的实现读取目录中的多个文件。我想我应该有一些目录列表,然后对每个文件进行种子设定 # -*- coding: UTF-8 -*- from __future__ import unicode_literals import csv from django.core.management.base import BaseCommand from elite_schedule.model

我在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

我将所有文件添加到一个数组中,并遍历该数组:

# -*- 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__)))

csv_files = ['bundesliga_1.csv','bundesliga_2.csv','eng_championship.csv','eng_conference.csv','eng_league_1.csv','eng_league_2.csv','eng_premier_league.csv','la_liga_primiera_division.csv',' La_Liga_Segunda Division.csv','seria_a.csv','seria_b.csv']

CSV_FOLDER_PATH = os.path.join(BASE_DIR, "data")

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)
        for csv_file in csv_files:

            file_path = CSV_FILE_PATH = os.path.join(CSV_FOLDER_PATH,str(csv_file))
            print(file_path)
            if verbosity >= NORMAL:
                self.stdout.write("=== Matches imported ===")
            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]
                    print(game)
                        # let's skip the column captions
                    # continue
                    """Assign country based on division.
                    to get divisions code details check football.uk
                    """ 
                    try: 

                        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
                        )
                        if verbosity >= NORMAL:
                            self.stdout.write("{}. {}".format(game, match.division))
                    except Exception as e:
                        raise e 

将读取一个文件的代码放入一个单独的方法中,并使用os.walk()遍历目录(将参数x更改为BASE_DIR)。python标准文档应该可以帮助您实现这一目标。