Home > Back-end >  How to correctly use multiple variables imported from a file in a python loop
How to correctly use multiple variables imported from a file in a python loop

Time:01-15

I'm exporting the first 10 results of a Google search with python code. But I have 100 different search queries and I don't want to change the query manually for every search. I had the idea to import my search queries from a .csv file and put them in a var. Like the code below:

import csv
import requests

with open('test.csv', 'r') as file:
    next(file) # drop header
    varlist = [row[0] for row in csv.reader(file, delimiter=",")]


for var in varlist:
    payload = {'api_key': 'API_KEY', 'query':str(var), 'results':'10', 'country':'gb', 'page':'0'}
    resp = requests.get('https://api.example.com', params=payload)
print (resp.text)

My .csv looks like:

varlist,customer
2132,1
1234,2

When i run my code above, I only get output for the search query '1234'.

output:

result search query 1234

expected output:

result search query 2132
result search query 1234

Where it goes wrong? And how to handle this correctly?

CodePudding user response:

You need to store the data in a python data structure like a list or dict:

data = []  # <- HERE
for var in varlist:
    payload = {'api_key': 'API_KEY', 'query':str(var), 'results':'10', 'country':'gb', 'page':'0'}
    resp = requests.get('https://api.example.com', params=payload)
    data.append(resp.text)  # <- HERE

print(*data, sep='\n')

# Output
result search query 2132
result search query 1234

Note: if your csv contains header, use DictReader class instead of reader function

CodePudding user response:

The solution is very simple. I had to put print inside the loop like in the code below:

for var in varlist:
    payload = {'api_key': 'API_KEY', 'query':str(var), 'results':'10', 'country':'gb', 'page':'0'}
    resp = requests.get('https://api.example.com', params=payload)
    print (resp.text)

Thanks everyone commenting on me

  • Related