Home > Software engineering >  Parse plaintext API response
Parse plaintext API response

Time:10-23

Im getting plaintext responses from an API like these:

enter image description here

So i would like to parse or pass those values to variables.

Example:

If the response is:

TD_OK
3213513 

I would like to convert this to:

TD_Result = TD_OK
TD_Number = 3213513

I tried something like this, but did not work:

result = """
TD_EXISTS
23433395"""

result2 = []

for r in result:
   result2.append(r)


TD_Result = result2[1]
TD_Number = result2[2]

print (TD_Result)
print (TD_Number)

Any idea about how to do that?

CodePudding user response:

for r in result: -> for r in result.splitlines():

CodePudding user response:

You can do this using the split method as follows.

Also note that list indexes in Python start at zero instead of one.

result = """
TD_EXISTS
23433395"""

result2 = result.split()

TD_Result = result2[0]
TD_Number = result2[1]

print (TD_Result)
print (TD_Number)
  • Related