在很多时候,程序代码中的变量很多时候都要用逗号隔开,例如下面的SQL语句:
SELECT xray_id,station,location,product_model,service_time,test_day,power_light,xray_light,entrance,exit_door,left_wall,right_wall,top,workbench,exposed_num,shift,unit_id,report_id FROM xray_info;
如果只有几个变量,写起来还可以接受,但有变量时候一大串,有几十个,手写很麻烦,可以用Python写个小脚本处理一下。
例如,我手头上的数据词典是这样的:
检测编号--xray_id
站点--station
位置--location
型号--product_model
使用时间--service_time
检测日期--test_day
通电指示灯--power_light
X线指示灯--xray_light
行李包入口--entrance
行李包出口--exit_door
旅客通过左侧面--left_wall
旅客通过右侧面--right_wall
行李包系统顶面--top
操作人员工作台--workbench
接触人数--exposed_num
工作班制--shift
单位ID--unit_id
报告ID--report_id
如果一个一个的复制写到程序中,是件十分麻烦的事情,把它复制到文本文件toList.txt中,然后用下面的脚本运行一下,即可。
with open('toList.txt', encoding='utf8') as file:
lines = file.readlines()
arg=[lines[i].strip().split('--')[1] for i in range(len(lines))]
print(','.join(arg))
结果