The first thing you need to do is know how to read each line from the file. You must open the file first; you can do this with the with statement:
with open('my-file-name.txt') as intfile:
This opens the file and saves the link to that file in the intfile , and it automatically closes the file at the end of your with block. Then you need to read each line from the file; you can do this with the usual old for loop:
for line in intfile:
This will go through each line in the file, reading them one at a time. In your loop, you can access each line as line . It remains only to make a request to your site using the code that you specified. One bit of your missing is what is called βstring interpolation," which allows you to format a string with other strings, numbers, or anything else. In your case, you want to put a line (a line from your file) inside another line (URL). To do this, you use the %s flag along with the string interpolation operator, % :
url = 'http://example.com/?id=%s' % line A = json.load(urllib.urlopen(url)) print A
Combining all this, you get:
with open('my-file-name.txt') as intfile: for line in intfile: url = 'http://example.com/?id=%s' % line A = json.load(urllib.urlopen(url)) print A
source share