简体   繁体   中英

How do I import a text file into a SQLite3 table using python

I currently have a txt file of movies which contain a movieID,Movie Name, and genre. Example:

1,Toy Story (1995),Animation|Children's|Comedy
2,Jumanji (1995),Adventure|Children's|Fantasy
3,Grumpier Old Men (1995),Comedy|Romance
4,Waiting to Exhale (1995),Comedy|Drama
5,Father of the Bride Part II (1995),Comedy

I am trying to import this file into a SQLite3 table created with Python:

import sqlite3

conn = sqlite3.connect('CSC 452 Final.db')
c=conn.cursor()

Drop Tables

c.execute('''DROP TABLE IF EXISTS temp_movie''')
print("Table temp_movie dropped.")

Create Table for Movies

c.execute('''CREATE TABLE temp_movie (
    MovieID          NUMERIC (5),
    movie_title_year VARCHAR2 (255),
    movie_categories VARCHAR2 (255),
    CONSTRAINT movies_movieID_PK PRIMARY KEY (
        MovieID
    ))''')
print("Table temp_movie successfully created.")
conn.commit()

What's the best way to import my text file into this database with the delimiter being ','?

You can read in the txt file and split on , :

file_data = [i.strip('\n').split(',') for i in open('filename.txt')]
new_data = [[int(a), *b] for a, *b in file_data] #convert string id to integer
conn = sqlite3.connect('CSC 452 Final.db')
c = conn.cursor()
c.executemany('INSERT INTO temp_movie VALUES (?, ?, ?)', new_data)

I would have used python csv module to do that:

with open(<txt file>,'rb') as f: 
    dic_reader = csv.DictReader(f)
    value_list = [(each_row['MovieID'], each_row['movie_title_year'], each_row['movie_title_year']) for each_row in dic_reader]

c.executemany("INSERT INTO t (MovieID, movie_title_year, movie_title_year) VALUES (?, ?);", value_list)
conn.commit()
conn.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM