70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
|
|
import os
|
|||
|
|
import logging
|
|||
|
|
from excel_stock_importer import ExcelStockImporter
|
|||
|
|
|
|||
|
|
# 设置日志
|
|||
|
|
logging.basicConfig(level=logging.INFO)
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
# 数据库配置
|
|||
|
|
db_config = {
|
|||
|
|
'host': '127.0.0.1',
|
|||
|
|
'database': 'fintech',
|
|||
|
|
'user': 'root',
|
|||
|
|
'password': 'secret',
|
|||
|
|
'port': 3306
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# Excel文件路径 - 尝试不同的路径
|
|||
|
|
excel_files = [
|
|||
|
|
"stock_list.xlsx", # 当前目录
|
|||
|
|
"./finance/stock_list.xlsx", # 当前目录(明确指定)
|
|||
|
|
os.path.join(os.getcwd(), "./finance/stock_list.xlsx"), # 绝对路径
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
print("=== 当前工作目录 ===")
|
|||
|
|
print(f"工作目录: {os.getcwd()}")
|
|||
|
|
print(f"目录内容: {os.listdir('.')}")
|
|||
|
|
|
|||
|
|
# 初始化导入器
|
|||
|
|
importer = ExcelStockImporter(db_config)
|
|||
|
|
|
|||
|
|
found_file = None
|
|||
|
|
for excel_file in excel_files:
|
|||
|
|
if os.path.exists(excel_file):
|
|||
|
|
found_file = excel_file
|
|||
|
|
print(f"找到文件: {excel_file}")
|
|||
|
|
break
|
|||
|
|
else:
|
|||
|
|
print(f"文件不存在: {excel_file}")
|
|||
|
|
|
|||
|
|
if not found_file:
|
|||
|
|
print("未找到stock_list.xlsx文件,请检查:")
|
|||
|
|
print("1. 文件名是否正确(包括扩展名)")
|
|||
|
|
print("2. 文件是否在当前目录")
|
|||
|
|
print("3. 文件权限是否正常")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 验证数据
|
|||
|
|
print("\n=== 验证数据 ===")
|
|||
|
|
validation_result = importer.validate_data(found_file)
|
|||
|
|
print(f"数据验证结果: {'通过' if validation_result['valid'] else '失败'}")
|
|||
|
|
|
|||
|
|
if 'total_rows' in validation_result:
|
|||
|
|
print(f"总行数: {validation_result['total_rows']}")
|
|||
|
|
print(f"行业列表: {validation_result.get('industries', [])}")
|
|||
|
|
print(f"交易所列表: {validation_result.get('exchanges', [])}")
|
|||
|
|
|
|||
|
|
if validation_result.get('duplicate_stocks'):
|
|||
|
|
print(f"重复股票代码: {validation_result['duplicate_stocks']}")
|
|||
|
|
|
|||
|
|
if validation_result.get('valid'):
|
|||
|
|
# 导入数据
|
|||
|
|
print("\n=== 导入数据 ===")
|
|||
|
|
success = importer.import_to_database(found_file)
|
|||
|
|
print(f"导入结果: {'成功' if success else '失败'}")
|
|||
|
|
else:
|
|||
|
|
print(f"数据验证失败: {validation_result.get('message', '未知错误')}")
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|