Перемещение информации из файла CSV в базу данных - PullRequest
0 голосов
/ 20 ноября 2018

Извините за весь приведенный ниже код, однако я хочу дать как можно больше контекста для вопроса. Я пытаюсь создать взять CSV-файл и поместить определенные заголовки в базу данных, которая затем хранится в словаре или списке. Я дошел до того, что мой код будет брать только необходимые заголовки, но когда я добавляю значения в базу данных и возвращаю, все, что я получаю при печати, это ({}, {}, [], {}).

import const 
import sqlite3 
import csv

SEP = ','

def get_pokemon_stats():
    """Reads the data contained in a pokemon data file and parses it into
    several data structures.

    Args:
        None

    Returns: a tuple of:
        -a dict where:
            -each key is a pokemon name (str)
            -each value is a tuple of the following stats:
                -pokemon name (str)
                -species_id (int)
                -height (float)
                -weight (float)
                -type_1 (str)
                -type_2 (str)
                -url_image (str)
                -generation_id (int)
                -evolves_from_species_id (str)
        -a dict where:
            -each key is a pokemon species_id (int)
            -each value is the corresponding pokemon name (str)
        -a list of all pokemon names (strs)
        -a dict where:
            -each key is a pokemon type (str). Note that type_1 and type_2
            entries are all considered types. There should be no special
            treatment for the type NA; it is considered a type as well.
            -each value is a list of all pokemon names (strs) that fall into
            the corresponding type
    """
    name_to_stats = {}
    id_to_name = {}
    names = []
    pokemon_by_type = {}
    DATA_FILENAME = 'pokemon.csv' 
    con = sqlite3.connect('poki.db')
    cur = con.cursor()    
    cur.execute('DROP TABLE IF EXISTS poki')
    cur.execute( ' CREATE TABLE  poki( pokemon TEXT, species_id INTEGER,'
             ' height REAL, weight REAL)' )

    values = ('INSERT INTO poki VALUES (?, ?, ?, ?)')

    with open('pokemon.csv', 'r') as csv_file:
        csv_reader =csv.reader(csv_file)
        for line in csv_reader:
            list_of_values = (line[1] ,line [2], line [3], line[4])

            cur.execute(values, list_of_values)
            return cur.fetchall()
    cur.close()
    con.commit()
    con.close()            
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...