import pandas as pd
from your_module import DB_Link

# 读取 Excel 表格数据,并去除两端空格和所有换行符,并将小数进行四舍五入到小数点后两位
df_excel = pd.read_excel('C:/Users\EDY\Desktop/给定清单列表.xlsx').applymap(lambda x: str(round(float(x), 2)).strip().replace('
', '') if isinstance(x, (float, int)) else str(x).strip().replace('
', ''))

# 打印表格输出内容
print('表格输出内容:')
print(df_excel)

# 连接数据库
db = DB_Link('guanyu_work_order')
cur = db.cur

# 查询数据库表数据,并去除两端空格和所有换行符,并将小数进行四舍五入到小数点后两位
sql = '''
SELECT t2_subquery.parent_type_name, t2_subquery.type_name, t1.fee_item_name, t1.fee_item_remark, t1.fee_item_unit, t1.fee_item_price
FROM guanyu_work_order.t_repair_fee_item t1
JOIN (
SELECT t1.id AS ID, t2.fee_type_name AS parent_type_name, t1.fee_type_name AS type_name
FROM guanyu_work_order.t_repair_fee_type AS t1
LEFT JOIN guanyu_work_order.t_repair_fee_type AS t2 ON t1.parent_id = t2.ID
WHERE t2.fee_type_name IS NOT NULL) AS t2_subquery ON t1.fee_type_id = t2_subquery.ID;
'''
cur.execute(sql)
rows = cur.fetchall()
db_data = [tuple(str(round(float(cell), 2)).strip().replace('
', '') if isinstance(cell, (float, int)) else str(cell).strip().replace('
', '') for cell in row) for row in rows]

# 关闭与数据库的连接
db.close_connect()

# 对比数据
output_data = []
for _, excel_row in df_excel.iterrows():
    excel_row_set = set(excel_row)
    found = False
    for db_row in db_data:
        db_row_set = set(db_row)
        if excel_row_set == db_row_set:
            found = True
            break
    if not found:
        output_data.append([str(cell) for cell in excel_row])

# 输出结果
count_ = 0
print('不一致的数据:')
for data in output_data:
    count_ += 1
    print(data)
print('共有不一致个数:', count_)

请确保将your_module替换为您实际的模块名或文件名,以便正确导入数据库连接类。 在代码中,我修改了df_exceldb_data的处理逻辑,将小数进行四舍五入到小数点后两位。使用round(float(x), 2)对表格数据和数据库表数据中的小数进行四舍五入处理。然后,代码继续执行对比数据和输出结果的部分,与之前的版本保持一致。


原文地址: https://www.cveoy.top/t/topic/ckDc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录