Moving info from CSV file to databse
Sorry about all the code below, however, I want ti give as much context for the question as possible. I am trying to create take a csv file and put certain headers into a database, which is then kept in a dictionary or a list. I have gotten to the point where my code will take only the headers needed but when I add the values into the database and returned, all I got when printed is ({}, {}, , {}).
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()
python sqlite
|
show 3 more comments
Sorry about all the code below, however, I want ti give as much context for the question as possible. I am trying to create take a csv file and put certain headers into a database, which is then kept in a dictionary or a list. I have gotten to the point where my code will take only the headers needed but when I add the values into the database and returned, all I got when printed is ({}, {}, , {}).
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()
python sqlite
1
well, you never do anything with thosedict
s and thatlist
, you just return them, so what were you expecting?
– juanpa.arrivillaga
Nov 20 '18 at 3:21
You're not actually reading from the database, only executing theINSERT INTO
command
– cricket_007
Nov 20 '18 at 3:29
Also, every time you run this script, it'll drop and recreate the entire table... Seems inefficient
– cricket_007
Nov 20 '18 at 3:30
How would I read from the database, I thought if I used fetch all I can see what was inside it.
– Meliodus123
Nov 20 '18 at 3:30
You need to runSELECT * FROM poki
, thenfetchall
will return stuff
– cricket_007
Nov 20 '18 at 3:31
|
show 3 more comments
Sorry about all the code below, however, I want ti give as much context for the question as possible. I am trying to create take a csv file and put certain headers into a database, which is then kept in a dictionary or a list. I have gotten to the point where my code will take only the headers needed but when I add the values into the database and returned, all I got when printed is ({}, {}, , {}).
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()
python sqlite
Sorry about all the code below, however, I want ti give as much context for the question as possible. I am trying to create take a csv file and put certain headers into a database, which is then kept in a dictionary or a list. I have gotten to the point where my code will take only the headers needed but when I add the values into the database and returned, all I got when printed is ({}, {}, , {}).
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()
python sqlite
python sqlite
edited Nov 20 '18 at 3:29
cricket_007
82.2k1143111
82.2k1143111
asked Nov 20 '18 at 3:03
Meliodus123Meliodus123
375
375
1
well, you never do anything with thosedict
s and thatlist
, you just return them, so what were you expecting?
– juanpa.arrivillaga
Nov 20 '18 at 3:21
You're not actually reading from the database, only executing theINSERT INTO
command
– cricket_007
Nov 20 '18 at 3:29
Also, every time you run this script, it'll drop and recreate the entire table... Seems inefficient
– cricket_007
Nov 20 '18 at 3:30
How would I read from the database, I thought if I used fetch all I can see what was inside it.
– Meliodus123
Nov 20 '18 at 3:30
You need to runSELECT * FROM poki
, thenfetchall
will return stuff
– cricket_007
Nov 20 '18 at 3:31
|
show 3 more comments
1
well, you never do anything with thosedict
s and thatlist
, you just return them, so what were you expecting?
– juanpa.arrivillaga
Nov 20 '18 at 3:21
You're not actually reading from the database, only executing theINSERT INTO
command
– cricket_007
Nov 20 '18 at 3:29
Also, every time you run this script, it'll drop and recreate the entire table... Seems inefficient
– cricket_007
Nov 20 '18 at 3:30
How would I read from the database, I thought if I used fetch all I can see what was inside it.
– Meliodus123
Nov 20 '18 at 3:30
You need to runSELECT * FROM poki
, thenfetchall
will return stuff
– cricket_007
Nov 20 '18 at 3:31
1
1
well, you never do anything with those
dict
s and that list
, you just return them, so what were you expecting?– juanpa.arrivillaga
Nov 20 '18 at 3:21
well, you never do anything with those
dict
s and that list
, you just return them, so what were you expecting?– juanpa.arrivillaga
Nov 20 '18 at 3:21
You're not actually reading from the database, only executing the
INSERT INTO
command– cricket_007
Nov 20 '18 at 3:29
You're not actually reading from the database, only executing the
INSERT INTO
command– cricket_007
Nov 20 '18 at 3:29
Also, every time you run this script, it'll drop and recreate the entire table... Seems inefficient
– cricket_007
Nov 20 '18 at 3:30
Also, every time you run this script, it'll drop and recreate the entire table... Seems inefficient
– cricket_007
Nov 20 '18 at 3:30
How would I read from the database, I thought if I used fetch all I can see what was inside it.
– Meliodus123
Nov 20 '18 at 3:30
How would I read from the database, I thought if I used fetch all I can see what was inside it.
– Meliodus123
Nov 20 '18 at 3:30
You need to run
SELECT * FROM poki
, then fetchall
will return stuff– cricket_007
Nov 20 '18 at 3:31
You need to run
SELECT * FROM poki
, then fetchall
will return stuff– cricket_007
Nov 20 '18 at 3:31
|
show 3 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385606%2fmoving-info-from-csv-file-to-databse%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385606%2fmoving-info-from-csv-file-to-databse%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
well, you never do anything with those
dict
s and thatlist
, you just return them, so what were you expecting?– juanpa.arrivillaga
Nov 20 '18 at 3:21
You're not actually reading from the database, only executing the
INSERT INTO
command– cricket_007
Nov 20 '18 at 3:29
Also, every time you run this script, it'll drop and recreate the entire table... Seems inefficient
– cricket_007
Nov 20 '18 at 3:30
How would I read from the database, I thought if I used fetch all I can see what was inside it.
– Meliodus123
Nov 20 '18 at 3:30
You need to run
SELECT * FROM poki
, thenfetchall
will return stuff– cricket_007
Nov 20 '18 at 3:31