我对我的一个 python 脚本有疑问。我正在使用库 untangle ( https://github.com/stchris/untangle ) 将 xml 配置文件导入并转换到主脚本中。
问题:我在配置文件中有多个用户的用户信息,并且我正在循环使用这些信息。它工作得很好,但是由于从 xml 文件中生成的对象的名称,它使脚本非常难看。
具体来说,这意味着我不能(或者我只是不知道如何)更改我想动态使用的对象的名称。
示例代码如下:
if employee == 0:
if str(configobj.config.modes.employee.employee_1.name.cdata) != '':
display.drawtext(0,0,str(configobj.config.modes.employee.employee_1.name.cdata),"7x13B",255,255,255,True)
if str(configobj.config.modes.employee.employee_1.line1.cdata) != '':
display.drawtext(int(configobj.config.modes.employee.employee_1.line1['x']),
int(configobj.config.modes.employee.employee_1.line1['y']),
if str(configobj.config.modes.employee.employee_1.line2.cdata) != '':
display.drawtext(int(configobj.config.modes.employee.employee_1.line2['x']),
int(configobj.config.modes.employee.employee_1.line2['y']),
if str(configobj.config.modes.employee.employee_1.line3.cdata) != '':
display.drawtext(int(configobj.config.modes.employee.employee_1.line3['x']),
int(configobj.config.modes.employee.employee_1.line3['y']))
displayimage = True
elif employee == 1:
if str(configobj.config.modes.employee.employee_2.name.cdata) != '':
display.drawtext(0,0,str(configobj.config.modes.employee.employee_2.name.cdata),"7x13B",255,255,255,True)
if str(configobj.config.modes.employee.employee_2.line1.cdata) != '':
display.drawtext(int(configobj.config.modes.employee.employee_2.line1['x']),
int(configobj.config.modes.employee.employee_2.line1['y']),
if str(configobj.config.modes.employee.employee_2.line2.cdata) != '':
display.drawtext(int(configobj.config.modes.employee.employee_2.line2['x']),
int(configobj.config.modes.employee.employee_2.line2['y']),
if str(configobj.config.modes.employee.employee_2.line3.cdata) != '':
display.drawtext(int(configobj.config.modes.employee.employee_2.line3['x']),
int(configobj.config.modes.employee.employee_2.line3['y']),
if str(configobj.config.modes.employee.employee_2.image.cdata) != '':
display.showimage(160,0,str(configobj.config.modes.employee.employee_2.image.cdata))
displayimage = True
这是很多重复的代码,数量不断变化。我该如何改进呢?