CSV: Difference between revisions
(Created page with "Reading a geoip database, nb uses pip install ipaddress <source lang="python"> import csv, sys, ipaddress, sys csvfilename = sys.argv[1] # "IP2LOCATION-LITE-DB1.CSV" input_...") |
Andre Castro (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
'''Convert a CSV file into a python list of dictionaries for each row''' | |||
For instance the following CSV: | |||
<pre> | |||
Title,Author,Year | |||
Think Python,Allen B. Downey,2004 | |||
Programming Computer Vision with Python, Jan Erik Solem,2012 | |||
</pre> | |||
Is converted onto: | |||
<pre> | |||
{'Author': 'Allen B. Downey', 'Title': 'Think Python', 'Year': '2004'} | |||
{'Author': ' Jan Erik Solem', 'Title': 'Programming Computer Vision with Python', 'Year': '2012'} | |||
</pre> | |||
<source lang=python> | |||
#!/usr/bin/env python3 | |||
import csv | |||
csvfilename = 'your.csv' | |||
with open(csvfilename, newline='') as csvfile: | |||
csvreader = csv.DictReader(csvfile) | |||
headers_as_keys_dict = [r for r in csvreader] | |||
print(headers_as_keys_dict) | |||
for entry in headers_as_keys_dict: | |||
print(entry) | |||
</source> | |||
----- | |||
Reading a geoip database, nb uses | Reading a geoip database, nb uses | ||
Revision as of 16:13, 27 May 2019
Convert a CSV file into a python list of dictionaries for each row
For instance the following CSV:
Title,Author,Year Think Python,Allen B. Downey,2004 Programming Computer Vision with Python, Jan Erik Solem,2012
Is converted onto:
{'Author': 'Allen B. Downey', 'Title': 'Think Python', 'Year': '2004'} {'Author': ' Jan Erik Solem', 'Title': 'Programming Computer Vision with Python', 'Year': '2012'}
#!/usr/bin/env python3
import csv
csvfilename = 'your.csv'
with open(csvfilename, newline='') as csvfile:
csvreader = csv.DictReader(csvfile)
headers_as_keys_dict = [r for r in csvreader]
print(headers_as_keys_dict)
for entry in headers_as_keys_dict:
print(entry)
Reading a geoip database, nb uses
pip install ipaddress
import csv, sys, ipaddress, sys
csvfilename = sys.argv[1] # "IP2LOCATION-LITE-DB1.CSV"
input_raw = raw_input("Give me an IP?")
input_raw = ".".join(input_raw.split(".")[:3] + ["0"])
# print input_raw
# sys.exit(0)
input_num =int(ipaddress.IPv4Address(unicode(input_raw)))
print "your ip address", input_raw, "is the same as", input_num
csvfile = open(csvfilename)
last_row = None
for row in csv.reader(csvfile):
from_ip, to_ip, cc, description = row[0], row[1], row[2], row[3]
from_ip = int(from_ip)
to_ip = int(from_ip)
if (last_row):
if input_num >= last_row[0] and input_num <= to_ip:
print last_row
#print "***" , from_ip, to_ip, cc, description
last_row = (from_ip, to_ip, cc, description)
# ipaddress.IPv4Address(from_ip)
# ipaddress.IPv4Address(int(to_ip))
# print from_ip, to_ip, cc