将DBF文件(dBase, FoxPro等)中的数据转换到SQLite,可遍历指定目录下所有的dbf文件。可参考以下程序,本程序参考了dbf-to-sqlite:
# _*_coding: utf-8 _*_
'''
@File: main.py
@Time: 2024/07/17
@Author: LionGIS
@Contact: [email protected]
@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)
标签:files,SQLite,name,FoxPro,db,dbf,path,table,DBF From: https://www.cnblogs.com/liongis/p/18306881