Create a dabatase from a textfile got an error message in python

Question:

I have a TXT file like these:

state sex year name count
AK F 1920 Mary 14
AK F 1920 Eva 12
AK M 1920 Ben 14
AK M 1920 Will 12

I have more than 500’000 rows and it has no header, but the first column is state, the second column is gender, third is year and fourth shows name and fifth shows how many babys were called like this. Now I would like to create a database with a table. The table is this TXT file. Many names occur more than once in a column, because it shows how many times in a year this name occurs.

I did it like this:

result = open("data/all.TXT", "r")

import sqlite3 as sq3
con = sq3.connect('popular.db')

con.execute('''CREATE TABLE popname  (
               state TEXT, 
               sex TEXT,
               year TEXT,
               forename TEXT
               count INTEGER,
               PRIMARY KEY(forename));''')

for index, row in result.iterrows():
    con.execute('INSERT INTO popname VALUES (?, ?, ?)', row)
con.commit();
con.close();

I did that but then it highlights result.iterrows and gave these Error message:

'_io.TextIOWrapper' object has no attribute 'iterrows'

The text file has no header, i just add it to make it easier to understand

Asked By: J_Martin

||

Answers:

Assuming your text file contains comma separated values:

with open("data/all.TXT", "r") as f:
    result = f.readlines()
result = [x.split(",") for x in result]

import sqlite3 as sq3

con = sq3.connect("popular.db")

con.execute(
    """
    CREATE TABLE IF NOT EXISTS popname  (
        id INTEGER PRIMARY KEY,
        state TEXT,
        sex TEXT,
        year TEXT,
        forename TEXT,
        count INTEGER
    );
    """
)

for i, (state, sex, year, forename, count) in enumerate(result):
    con.execute(
        """
        INSERT INTO popname VALUES (?, ?, ?, ?, ?, ?);
        """,
        (i, state, sex, year, forename, count.strip()),
    )
    con.commit()

Note that you cannot set PRIMARY KEY(forename)) since it’s very likely some names will repeat and it will violate the UNIQUE constraint.

EDIT: I added column id and used the row number as value.

Answered By: Deneb
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.