简体   繁体   中英

How to correctly use multiple variables imported from a file in a python loop

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM