Python与SQL Server数据库结合导出Excel并做部分修改
需求:在数据库中提取需要的字段内容;并根据字段内容来提取与拆分数据做为新的列最后导出到Excel文件
import pandas as pd
import re
import pymssql
import timestart_time = time.time()
print("程序开始时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time)))
conn = pymssql.connect(server='192.168.2.1', user='sa', password='123', database='YD')
query = f'''
SELECT 类型,流水号,账号,时间,通过时间,客服号,地市,区县,grid,测试结果
FROM TS_DATA WHERE CAST(最后质检通过时间 AS date) = '2024-09-01';
'''
df = pd.read_sql(query, conn)
df['测试结果'] = df['测试结果'].astype(str)
def extract_info(text):light_power = re.search(r'【功率】:([^【\n]*)', text)light_power = light_power.group(1).strip() if light_power else Nonerate = re.search(r'【速率】:([\d.]+M)', text)rate = rate.group(1) if rate else Noneradius = re.search(r'【ra】:([^,\s【]+)', text)radius = radius.group(1).strip() if radius else Noneonline_time = re.search(r'上线:([\d/:\s]+)', text)online_time = online_time.group(1) if online_time else Nonereturn pd.Series([light_power, rate, radius, online_time],index=['功率', '速率', 'ra', '上线'])
df[['功率', '速率', 'ra', '上线']] = df['测试结果'].apply(extract_info)df.fillna('空白', inplace=True)
df['测试结果'] = df['测试结果'].replace('None', '', regex=False)
df['是否包含空白'] = (df['功率'] == "空白") | (df['速率'] == "空白") | (df['ra'] == "空白")
df['是否包含空白'] = df['是否包含空白'].map({True: '是', False: '否'})
output_file = '投诉9月份数据-0901.xlsx'
df.to_excel(output_file, index=False, engine='openpyxl')print(f"数据已处理并保存到 {output_file}")
conn.close()
end_time = time.time()
print("程序结束时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time)))
run_time = end_time - start_time
print("程序运行耗时:%0.2f" % run_time, "s")
最终效果图