input_file.txt在
之下'123','345','567'|001
'1234','3456','5678'|0011
下面是我用来将input_file.txt中的值分配给v_order_nbr_lst的代码,这个代码工作正常,但是当我在POST消息API调用中使用该变量时,它正在添加双引号。
fileHandle = open(input_file.txt,'r')
for line in fileHandle.readlines()[0:]:
line = line.rstrip('\n')
fields = line.split('|')
v_order_nbr_lst = (fields[0])
postdata = {
"parameters": [
{
"Order_Nbr": [v_order_nbr_lst]
}
]
}
现在我正在使用这样的双引号获取值,我试图替换& rstrip但不能摆脱"。
postdata = {
"parameters": [
{
"Order_Nbr": ["'123','345','567'"]
}
]
}
预期结果
postdata = {
"parameters": [
{
"Order_Nbr": ['123','345','567']
}
]
}
答案 0 :(得分:1)
v_order_nbr_lst
中的值不是您的想法,请看:
line = "'123','345','567'|001"
fields = line.split('|')
v_order_nbr_lst = fields[0]
v_order_nbr_lst
=> "'123','345','567'" # this is a string, not a list!
请改为尝试:
v_order_nbr_lst = fields[0].replace("'", '').split(',')
并指定如下值:
"Order_Nbr": v_order_nbr_lst