Skip to content

Commit 13df8e6

Browse files
committed
add comments
1 parent 8413111 commit 13df8e6

File tree

1 file changed

+10
-0
lines changed
  • Python/CSV-IP-geolocation

1 file changed

+10
-0
lines changed

Python/CSV-IP-geolocation/app.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import csv
22
import requests
33

4+
# Create a context manager to open `.csv` file.
45
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.
58
reader = csv.DictReader(csvfile)
9+
10+
# Iterate through the rows.
611
for row in reader:
12+
# Get the IP address from the `IP` column.
713
ip_address = row['IP']
14+
15+
# Generate the IP URL and perform a GET request.
816
api_url = "http://ip-api.com/json/" + ip_address
917
response = requests.get(api_url).json()
1018

19+
# extract important info to make code readable.
1120
city = response['city']
1221
state = response['regionName']
1322
country = response['country']
1423

24+
# Print results to console output.
1525
print(f"{ip_address} => {city}, {state}, {country}")

0 commit comments

Comments
 (0)