File tree 1 file changed +10
-0
lines changed
Python/CSV-IP-geolocation
1 file changed +10
-0
lines changed Original file line number Diff line number Diff line change 1
1
import csv
2
2
import requests
3
3
4
+ # Create a context manager to open `.csv` file.
4
5
with open ("IP_data.csv" ) as csvfile :
6
+ # Create an iterator which iterates through the rows of the file
7
+ # and returns the rows as dictionaries when invoked.
5
8
reader = csv .DictReader (csvfile )
9
+
10
+ # Iterate through the rows.
6
11
for row in reader :
12
+ # Get the IP address from the `IP` column.
7
13
ip_address = row ['IP' ]
14
+
15
+ # Generate the IP URL and perform a GET request.
8
16
api_url = "http://ip-api.com/json/" + ip_address
9
17
response = requests .get (api_url ).json ()
10
18
19
+ # extract important info to make code readable.
11
20
city = response ['city' ]
12
21
state = response ['regionName' ]
13
22
country = response ['country' ]
14
23
24
+ # Print results to console output.
15
25
print (f"{ ip_address } => { city } , { state } , { country } " )
You can’t perform that action at this time.
0 commit comments