|
@@ -97,6 +97,25 @@ def data_process():
|
|
|
|
|
|
# 读取Excel文件中的数据并进行预处理
|
|
|
df = pd.read_excel(io=input_path) # 读取Excel文件
|
|
|
+ # 获取当前 DataFrame 的列名列表
|
|
|
+ columns = df.columns.tolist()
|
|
|
+ # 定义所需的字段列表
|
|
|
+ required_columns = ['资产所属单位(一级)', '资产所属单位(二级)', '资产所属单位(三级)', '建筑别名', '盘点状态',
|
|
|
+ '盘点情况', '是否修改', '建筑是否存在待核对信息', '楼层房间是否存在待核对信息', '建筑ID',
|
|
|
+ '得房率', '上级局址名称', '局址ID', '上级土地名称', '房屋来源', '取得日期', '房龄开始年份',
|
|
|
+ '投资主体', '管理层级', '房屋结构', '楼层总数', '是否临街', '是否有院落', '整栋是否独有',
|
|
|
+ '是否有房产证', '无房产证原因', '是否有资产卡片', '资产编号', '资产标签号', '使用状态',
|
|
|
+ '建筑用途', '权属状态', '建筑占地面积(㎡)', '建筑面积(㎡)', '建筑面积-自用(㎡)',
|
|
|
+ '建筑面积-出租(㎡)', '建筑面积-闲置(㎡)', '建筑面积-不可使用(㎡)', '使用面积(㎡)',
|
|
|
+ '使用面积-自用(㎡)', '使用面积-出租(㎡)', '使用面积-闲置(㎡)', '使用面积-不可使用(㎡)', '楼长姓名',
|
|
|
+ '楼长所在单位', '经度', '纬度', '标准地址', '实际产权人', '是否完成清查']
|
|
|
+ # 检查是否有缺失的字段
|
|
|
+ missing_columns = [col for col in required_columns if col not in columns]
|
|
|
+ # 检查是否有多余的字段
|
|
|
+ ex_columns = [col for col in columns if col not in required_columns]
|
|
|
+ # 如果存在缺失字段,则抛出运行时错误并提示缺少哪些字段
|
|
|
+ if missing_columns or ex_columns:
|
|
|
+ raise RuntimeError(f"缺少以下字段: {missing_columns};存在以下多余字段:{ex_columns}")
|
|
|
df = df.map(lambda x: re.sub(r'\s+', '', x) if type(x) is str else x) # 去除字符串字段中的多余空格
|
|
|
df.drop_duplicates(subset=['建筑ID'], keep='last', inplace=True) # 去重,保留最后一条记录
|
|
|
|
|
@@ -117,22 +136,22 @@ def data_process():
|
|
|
return area_no
|
|
|
raise RuntimeError(f'二级组织机构编码匹配失败: {second_unit}')
|
|
|
|
|
|
- df['二级组织机构编码'] = df.apply(get_area_no, axis=1) # 应用函数,生成二级组织机构编码列
|
|
|
+ df['area_no'] = df.apply(get_area_no, axis=1) # 应用函数,生成二级组织机构编码列
|
|
|
|
|
|
# 定义函数:根据二级组织机构编码获取二级组织机构名称
|
|
|
def get_area_name(x):
|
|
|
- area_no = x['二级组织机构编码']
|
|
|
+ area_no = x['area_no']
|
|
|
second_org = org_map[area_no]
|
|
|
area_name = second_org['name']
|
|
|
return area_name
|
|
|
|
|
|
- df['二级组织机构名称'] = df.apply(get_area_name, axis=1) # 应用函数,生成二级组织机构名称列
|
|
|
+ df['area_name'] = df.apply(get_area_name, axis=1) # 应用函数,生成二级组织机构名称列
|
|
|
|
|
|
# 定义函数:根据资产所属单位获取三级组织机构编码
|
|
|
def get_city_no(x):
|
|
|
third_unit = x['资产所属单位(三级)']
|
|
|
- area_name = x['二级组织机构名称']
|
|
|
- area_no = x['二级组织机构编码']
|
|
|
+ area_name = x['area_name']
|
|
|
+ area_no = x['area_no']
|
|
|
if area_name == '石家庄':
|
|
|
if '矿区' in third_unit:
|
|
|
return 'D0130185'
|
|
@@ -215,16 +234,16 @@ def data_process():
|
|
|
return 'D0130830'
|
|
|
return 'HE001'
|
|
|
|
|
|
- df['三级组织机构编码'] = df.apply(get_city_no, axis=1) # 应用函数,生成三级组织机构编码列
|
|
|
+ df['city_no'] = df.apply(get_city_no, axis=1) # 应用函数,生成三级组织机构编码列
|
|
|
|
|
|
# 定义函数:根据三级组织机构编码获取三级组织机构名称
|
|
|
def get_city_name(x):
|
|
|
- city_no = x['三级组织机构编码']
|
|
|
+ city_no = x['city_no']
|
|
|
third_org = org_map[city_no]
|
|
|
city_name = third_org['name']
|
|
|
return city_name
|
|
|
|
|
|
- df['三级组织机构名称'] = df.apply(get_city_name, axis=1) # 应用函数,生成三级组织机构名称列
|
|
|
+ df['city_name'] = df.apply(get_city_name, axis=1) # 应用函数,生成三级组织机构名称列
|
|
|
|
|
|
# 定义函数:根据标准地址获取城市ID
|
|
|
def get_city_id(x):
|
|
@@ -328,30 +347,45 @@ def data_process():
|
|
|
return ''
|
|
|
|
|
|
df['house_age'] = df.apply(get_house_age, axis=1) # 应用函数,生成房龄列
|
|
|
- df.insert(0, '年月', year_month) # 在数据框第一列插入年月列
|
|
|
-
|
|
|
+ df.insert(0, 'year_month', year_month) # 在数据框第一列插入年月列
|
|
|
+ df.rename(
|
|
|
+ columns={'资产所属单位(一级)': 'first_unit', '资产所属单位(二级)': 'second_unit', '资产所属单位(三级)': 'third_unit',
|
|
|
+ '建筑别名': 'building_name', '盘点状态': 'inventory_status', '盘点情况': 'inventory_situation',
|
|
|
+ '是否修改': 'modify', '建筑是否存在待核对信息': 'building_to_be_verified',
|
|
|
+ '楼层房间是否存在待核对信息': 'floor_room_to_be_verified', '建筑ID': 'building_id',
|
|
|
+ '得房率': 'housing_acquisition_rate', '上级局址名称': 'site_name', '局址ID': 'site_id',
|
|
|
+ '上级土地名称': 'land_name', '房屋来源': 'housing_source', '取得日期': 'acquisition_date',
|
|
|
+ '房龄开始年份': 'house_year_began', '投资主体': 'investor', '管理层级': 'management_level',
|
|
|
+ '房屋结构': 'building_structure', '楼层总数': 'total_floors', '是否临街': 'frontage',
|
|
|
+ '是否有院落': 'courtyard', '整栋是否独有': 'whole_building',
|
|
|
+ '是否有房产证': 'property_ownership_certificate',
|
|
|
+ '无房产证原因': 'no_property_ownership_certificate_reason', '是否有资产卡片': 'has_asset_card',
|
|
|
+ '资产编号': 'assets_num', '资产标签号': 'assets_tag_num', '使用状态': 'usage_status',
|
|
|
+ '建筑用途': 'building_use', '权属状态': 'ownership_status', '建筑占地面积(㎡)': 'floor_area',
|
|
|
+ '建筑面积(㎡)': 'building_area', '建筑面积-自用(㎡)': 'building_area_self_use',
|
|
|
+ '建筑面积-出租(㎡)': 'building_area_rent', '建筑面积-闲置(㎡)': 'building_area_idle',
|
|
|
+ '建筑面积-不可使用(㎡)': 'building_area_unusable', '使用面积(㎡)': 'usable_area',
|
|
|
+ '使用面积-自用(㎡)': 'usable_area_self_use', '使用面积-出租(㎡)': 'usable_area_rent',
|
|
|
+ '使用面积-闲置(㎡)': 'usable_area_idle', '使用面积-不可使用(㎡)': 'usable_area_unusable',
|
|
|
+ '楼长姓名': 'community_assistant_name', '楼长所在单位': 'community_assistant_unit', '经度': 'lng_jt',
|
|
|
+ '纬度': 'lat_jt', '标准地址': 'address', '实际产权人': 'property_owner', '是否完成清查': 'checked'}, inplace=True)
|
|
|
+ df = df[['year_month', 'first_unit', 'second_unit', 'third_unit', 'building_name', 'inventory_status',
|
|
|
+ 'inventory_situation', 'modify', 'building_to_be_verified', 'floor_room_to_be_verified', 'building_id',
|
|
|
+ 'housing_acquisition_rate', 'site_name', 'site_id', 'land_name', 'housing_source', 'acquisition_date',
|
|
|
+ 'house_year_began', 'investor', 'management_level', 'building_structure', 'total_floors', 'frontage',
|
|
|
+ 'courtyard', 'whole_building', 'property_ownership_certificate',
|
|
|
+ 'no_property_ownership_certificate_reason', 'has_asset_card', 'assets_num', 'assets_tag_num',
|
|
|
+ 'usage_status', 'building_use', 'ownership_status', 'floor_area', 'building_area',
|
|
|
+ 'building_area_self_use', 'building_area_rent', 'building_area_idle', 'building_area_unusable',
|
|
|
+ 'usable_area', 'usable_area_self_use', 'usable_area_rent', 'usable_area_idle', 'usable_area_unusable',
|
|
|
+ 'community_assistant_name', 'community_assistant_unit', 'lng_jt', 'lat_jt', 'address',
|
|
|
+ 'property_owner', 'checked', 'area_no', 'area_name', 'city_no', 'city_name', 'city_id', 'city',
|
|
|
+ 'district_id', 'district', 'year_no', 'month_no', 'house_age']]
|
|
|
# 打印数据框信息
|
|
|
- print(df.info())
|
|
|
+ df.info()
|
|
|
|
|
|
# 将结果保存为CSV文件
|
|
|
- df.to_csv(
|
|
|
- path_or_buf=output_path,
|
|
|
- index=False,
|
|
|
- header=[
|
|
|
- 'year_month', 'first_unit', 'second_unit', 'third_unit', 'building_name', 'building_id',
|
|
|
- 'housing_acquisition_rate', 'site_name', 'site_id', 'land_name', 'housing_source', 'acquisition_date',
|
|
|
- 'house_year_began', 'investor', 'management_level', 'building_structure', 'total_floors', 'frontage',
|
|
|
- 'courtyard', 'whole_building', 'property_ownership_certificate',
|
|
|
- 'no_property_ownership_certificate_reason', 'unrelated_assets', 'assets_num', 'assets_tag_num',
|
|
|
- 'usage_status', 'building_use', 'ownership_status', 'floor_area', 'building_area',
|
|
|
- 'building_area_self_use', 'building_area_rent', 'building_area_idle', 'building_area_unusable',
|
|
|
- 'usable_area', 'usable_area_self_use', 'usable_area_rent', 'usable_area_idle', 'usable_area_unusable',
|
|
|
- 'community_assistant_name', 'community_assistant_unit', 'lng_jt', 'lat_jt', 'address',
|
|
|
- 'property_owner', 'checked', 'area_no', 'area_name', 'city_no', 'city_name', 'city_id', 'city',
|
|
|
- 'district_id', 'district', 'year_no', 'month_no', 'house_age'
|
|
|
- ],
|
|
|
- encoding='utf-8-sig'
|
|
|
- )
|
|
|
+ df.to_csv(path_or_buf=output_path, index=False, encoding='utf-8-sig', lineterminator='\n')
|
|
|
|
|
|
|
|
|
def data_import():
|
|
@@ -360,9 +394,9 @@ def data_import():
|
|
|
# 目标表和文件信息
|
|
|
table = "house.building_month" # 数据库目标表名
|
|
|
# 表字段列名,用于指定导入数据的列顺序
|
|
|
- columns = "year_month,first_unit,second_unit,third_unit,building_name,building_id,housing_acquisition_rate,site_name,site_id,land_name,housing_source,acquisition_date,house_year_began,investor,management_level,building_structure,total_floors,frontage,courtyard,whole_building,property_ownership_certificate,no_property_ownership_certificate_reason,unrelated_assets,assets_num,assets_tag_num,usage_status,building_use,ownership_status,floor_area,building_area,building_area_self_use,building_area_rent,building_area_idle,building_area_unusable,usable_area,usable_area_self_use,usable_area_rent,usable_area_idle,usable_area_unusable,community_assistant_name,community_assistant_unit,lng_jt,lat_jt,address,property_owner,checked,area_no,area_name,city_no,city_name,city_id,city,district_id,district,year_no,month_no,house_age"
|
|
|
+ columns = "year_month,first_unit,second_unit,third_unit,building_name,inventory_status,inventory_situation,modify,building_to_be_verified,floor_room_to_be_verified,building_id,housing_acquisition_rate,site_name,site_id,land_name,housing_source,acquisition_date,house_year_began,investor,management_level,building_structure,total_floors,frontage,courtyard,whole_building,property_ownership_certificate,no_property_ownership_certificate_reason,has_asset_card,assets_num,assets_tag_num,usage_status,building_use,ownership_status,floor_area,building_area,building_area_self_use,building_area_rent,building_area_idle,building_area_unusable,usable_area,usable_area_self_use,usable_area_rent,usable_area_idle,usable_area_unusable,community_assistant_name,community_assistant_unit,lng_jt,lat_jt,address,property_owner,checked,area_no,area_name,city_no,city_name,city_id,city,district_id,district,year_no,month_no,house_age"
|
|
|
# 构造执行 PowerShell 脚本的命令
|
|
|
- command = f"powershell -File {script_path} -db_host {db_host} -db_port {db_port} -db_username {db_username} -db_password {db_password} -dbname {dbname} -table {table} -filename {output_path} -columns {columns}"
|
|
|
+ command = f"powershell -NoProfile -NonInteractive -File {script_path} -db_host {db_host} -db_port {db_port} -db_username {db_username} -db_password {db_password} -dbname {dbname} -table {table} -filename {output_path} -columns {columns}"
|
|
|
# 打印生成的命令,方便调试和日志记录
|
|
|
logger.info("command: {}", command)
|
|
|
# 使用 subprocess 模块运行 PowerShell 命令,并捕获输出
|