rkj
rkj

Reputation: 711

Python variable value adding a double quotes

input_file.txt has below

'123','345','567'|001
'1234','3456','5678'|0011

below is the code I am using to assign the values from input_file.txt to v_order_nbr_lst, which is working fine , but when I am using that variable inside a POST message API call like below , it is adding double quotes.

read the input file

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])

variable used below

postdata = {
    "parameters": [
        {
        "Order_Nbr": [v_order_nbr_lst]
             }
          ]
     }

Now I am getting values with double quotes like this , i tried to replace & rstrip but not able to get rid of ".

postdata = {
    "parameters": [
        {
        "Order_Nbr": ["'123','345','567'"]
             }
          ]
     }

expected result

postdata = {
    "parameters": [
        {
        "Order_Nbr": ['123','345','567']
             }
          ]
     }

Upvotes: 1

Views: 113

Answers (1)

Óscar López
Óscar López

Reputation: 236004

The value in v_order_nbr_lst is not what you think, look:

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!

Try this instead:

v_order_nbr_lst = fields[0].replace("'", '').split(',')

And assign the value like this:

"Order_Nbr": v_order_nbr_lst

Upvotes: 1

Related Questions