如何在python中比较字符串并给出字符串输出

2024-06-16 10:56:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个工具来检查不同设备的正确设备信息。 正如您在这里看到的,整个结果要么追加到文件中,要么作为1或0写入文件中。 理想情况下,它应该做的是追加或写下PASS或FAIL,在这种情况下,1是PASS,0是FAIL。谁能给我一些建议,我如何才能做到这一点。或者,如果你们专业人士能给我一个示例代码块,我也会非常感激

到目前为止,我所尝试的: 我将OVERALL_STATUS = 1设置为OVERALL_STATUS = PASS{} DEVICE_TYPE_STATUS = 0在其他之后。但是它没有打印出正确的答案。 打印“整体结果通过”, 但事实上这不是“失败”

请对我放松点,伙计们,我为没有使用正确的术语提前道歉


OVERALL_STATUS = 1
STATUS_LIST = []
DEVICE_TYPE_STATUS = 1
DEVICE_TYPE_LIST = []

# Order number
print("Desired Order number:",d_ordernum)
print("Order number from scancode :",scan_code_cropped_artikel)
print("Ordernumber from wbm: ", ord_nmr)
if d_ordernum == ord_nmr == scan_code_cropped_artikel:
    print("Order number PASS")
else:
    print("Order number FAIL")
    OVERALL_STATUS = 0
    STATUS_LIST.append('Order Number FAIL')
print(100*"")

print("Desired device type:",d_dev_typ)
print("Device type from wbm: ", dev_typ)

if d_dev_typ == dev_typ:
   print("Device type PASS") 
else:
    print("Device type FAIL")
    OVERALL_STATUS = 0
    DEVICE_TYPE_STATUS = 0
    STATUS_LIST.append("Device type FAIL")
print(100*"")

if OVERALL_STATUS:
    print('Overall Result PASS')
else:
    print('Overall Result FAIL, ' + ', '.join(STATUS_LIST))



header = ["Timestamp:","Overall result:","Soll-orderno:","Desired-HW-Version:","Desired-SF-Version:","Desired-productcode:","Desired-device-type:","Scancode:","Wbm-orderno:","Wbm-HW-Version:","Wbm-SF-Version:","Wbm-mac-address:","combined-product-code:","wbm-device-type:","test-device-type:"]
toWrite = [
    [now ,OVERALL_STATUS,d_ordernum,d_hw_version,d_sf_version,pc_praefix,d_dev_typ,scancode_string,ord_nmr,v,b,mac_addr,product_code,dev_typ,DEVICE_TYPE_STATUS]
] 

file_exists = os.path.isfile(d_output_file)
    
if file_exists:
    with open (d_output_file, 'a') as f:
         writer = csv.writer(f)
         writer.writerows(toWrite)


if not file_exists:
    with open (d_output_file, 'a') as f:
        writer = csv.writer(f)
        writer.writerow(header)  
        writer.writerows(toWrite)

CSV文件中的输出:

Timestamp:,Overall result:,Soll-orderno:,Desired-HW-Version:,Desired-SF-Version:,Desired-productcode:,Desired-device-type:,Scancode:,Wbm-orderno:,Wbm-HW-Version:,Wbm-SF-Version:,Wbm-mac-address:,combined-product-code:,wbm-device-type:,test-device-type:

2021-05-03 14:55:46,0,58184,1.0,1.0.0,7A2F7,TREE M-5TX PN IP67,58184#99AF0M000F9EF41A80,58184,1.00,1.0.0,00:0F:9E:F4:1A:80,7A2F7-1.0.0-1.00,TREE M-5TX PN IP67,1

2021-05-03 15:04:21,0,58184,1.0,1.0.0,7A2F7,TREE M-5TX PN IP67,58184#99AF0M000F9EF41A80,58184,1.00,1.0.0,00:0F:9E:F4:1A:80,7A2F7-1.0.0-1.00,TREE M-5TX PN IP67,1

2021-05-03 15:06:05,0,58184,1.0,1.0.0,7A2F7,TREE M-5TX PN IP67,58184#99AF0M000F9EF41A80,58184,1.00,1.0.0,00:0F:9E:F4:1A:80,7A2F7-1.0.0-1.00,TREE M-5TX PN IP67,1

Tags: devversiondevicetypestatusorderpassfile
1条回答
网友
1楼 · 发布于 2024-06-16 10:56:41

在if-else中,如果要打印pass或fail,则可以使用string更改总体状态的值,否则可以创建新变量并将其写入文件

string_status = ''
if OVERALL_STATUS:
    print('Overall Result PASS')
    string_status = 'PASS'
else:
    print('Overall Result FAIL, ' + ', '.join(STATUS_LIST))
    string_status = 'Fail'

现在将toWrite更改为:

toWrite = [
    [now ,string_status,d_ordernum,d_hw_version,d_sf_version,pc_praefix,d_dev_typ,scancode_string,ord_nmr,v,b,mac_addr,product_code,dev_typ,DEVICE_TYPE_STATUS]
]

相关问题 更多 >