Using pipe delimited string from requests.get - python

Viewed 19

I am using the requests module to pull data from a website. The data is being passed as a pipe delimited string. The data has headings and each request can have multiple rows returned.

The data looks as follows for example: name|surname|ID|product David|John|1|apple Paul|Smith|2|orange .....

I need to be able to access each variable within the above string so that I can pass it through to another API. For example, the API call will need to loop through each line in the above string:

for line in x

post = {
         "name": line.["name"],
         "surname": line.["surname"],
         "ID": line.["ID"],
         "product": line.["product"]
       }

I would like to run the code using a cron job, so I would prefer to avoid creating a file and then working with the file.

TIA

1 Answers

I managed to solve this - for whoever else comes across this problem:

url = your_requested_url

result = requests.get(url)
string_output = result.text
for line in string_output.splitlines():
   mystring = line.split("|")
   post = {
       "name": mystring.[0],
       "surname": mystring.[1],
       "ID": mystring.[2],
       "product": mystring.[3]
                }
Related