简体   繁体   中英

Reading Text file with delimiter to sqlite using Python

Sample Data
Above is my sample text file with ! as delimiter

I want the data Table to look this image below Sqlite

Expected result Please, how can i insert this data into sqlite in python here is my code

    with open('corp.fr') as f:
    mylist = [tuple(x.split('!')) for x in f]
    conn = sqlite3.connect("alignmentset.db")
    conn.execute("DROP TABLE IF EXISTS native_align")
    conn.execute("DROP TABLE IF EXISTS foreign_align")
    conn.execute("CREATE TABLE native_align (id INTEGER PRIMARY KEY AUTOINCREMENT, nlang TEXT)")
    conn.execute("CREATE TABLE foreign_align (id INTEGER PRIMARY KEY AUTOINCREMENT, flang TEXT)")
    conn.executemany("INSERT INTO native_align (nlang) VALUES (?)", (mylist))
    conn.executemany("INSERT INTO foreign_align (flang) VALUES (?)", (mylist))
    conn.commit()

please help

Question : Reading Text file with delimiter ... I want the data to split into two SQLITE tables.

  1. Read your Text file line by line

     for line in f: ... 
  2. Use a Condition to decide which line goes into which sqlite table .

     if line is 'Matiyu 1': ... elif line is '!Mattieu Chapitre 1': ... 
  3. Insert the actual line into the sqlite table

  4. Repeat from 1. until all lines from Text file read.

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