将DBF文件(dBase, FoxPro等)中的数据转换到SQLite,可遍历指定目录下所有的dbf文件。可参考以下程序,本程序参考了dbf-to-sqlite: 

# _*_coding: utf-8 _*_
'''
@File:        main.py
@Time:        2024/07/17
@Author:      LionGIS
@Contact:     liongis@163.com
@Description: 将DBF文件(dBase, FoxPro等)中的数据转换到SQLite
'''

import fnmatch
import os
import click
import dbf
from pathlib import Path
from sqlite_utils import Database

def main(dbf_paths, sqlite_db, table_name=None, verbose=None):
    """
    Convert DBF files (dBase, FoxPro etc) to SQLite
    https://github.com/simonw/dbf-to-sqlite
    """
    db = Database(sqlite_db)

    db_files = []
    # 遍历目录
    # for root, dirs, files in os.walk(dbf_paths):
    files = os.listdir(dbf_paths)
    for file in files:
        if fnmatch.fnmatch(file, '*.dbf'):
            if table_name:
                if (table_name + ".dbf").lower() == file.lower():
                    db_files.append(os.path.join(dbf_paths, file))
            else:
                db_files.append(os.path.join(dbf_paths, file))

    for path in db_files:
        table_name = Path(path).stem
        if verbose:
            click.echo('加载 {} 插入表 "{}"'.format(path, table_name))
        table = dbf.Table(str(path))
        table.open()
        columns = table.field_names
        db[table_name].insert_all(dict(zip(columns, list(row))) for row in table)
        table.close()
    db.vacuum()

if __name__ == "__main__":
    # 根目录
    base_path = r"c:/demo/"

    #  dbf文件路径
    dbf_path = base_path + ""
    #  sqlite文件路径 + 文件名
    sqlite_db = base_path + "database.db"
    #  表名,None为所有dbf文件
    table_name = None
    main(dbf_path, sqlite_db, table_name, True)

 

posted on 2024-07-17 11:22  liongis  阅读(3)  评论(0编辑  收藏  举报