I've got some files which can help a little bit to figure out where people are from based on their ID card NO.

 

That file looks like this:

Then I converted it into *.csv format which is basically a text file.

    It's not hard that almost every common document editor has this functionality.

xls===>csv tables===via python ===> sqlite3.db

Here is my python codes:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
    To store information into a sqlite database
    Usage: $ python id2sql.py afile.csv idlist.db
    This will invoke afile.csv to create a new database named idlist.db

                                    ----  Alex Liu
    
'''
import sqlite3 as dbapi
import csv
import sys


def createDB(path, destination):
    '''
        use the *.csv path to create a database file
    '''
    csvfilepath = path
    
    con = dbapi.connect(destination)
    con.text_factory = str
    cur = con.cursor()
    cur.execute('CREATE TABLE idtable(code INTEGER, Region TEXT)')
    try:
        with open(csvfilepath,'rb') as idcsv:        # 'rb' coz file csv is an obj
            spamreader = csv.reader( idcsv, delimiter=',', quotechar='|')
            for row in spamreader:
                cur.execute( 'INSERT INTO idtable VALUES (?,?)',( row[1],row[2]) )
            con.commit()    # To update database
        return "database %s updated! :)" % destination
    except:
        return "check the source codes again :("

if __name__=="__main__":
    print createDB(sys.argv[1], sys.argv[2])
        

Run it:

xls===>csv tables===via python ===> sqlite3.db

Then, use the sqlite brrowser to check it out:

 

xls===>csv tables===via python ===> sqlite3.db

You could see the whole content of it :)

xls===>csv tables===via python ===> sqlite3.db

 

Isn't good ??

 

Ha Ha Have fun!!

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-16
  • 2021-12-17
  • 2021-11-12
  • 2021-06-02
  • 2021-06-10
  • 2021-07-05
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
相关资源
相似解决方案