CSV: Difference between revisions
Andre Castro (talk | contribs) No edit summary |
No edit summary |
||
Line 16: | Line 16: | ||
</pre> | </pre> | ||
== Reading CSV as a dictionary...== | |||
<source lang=python> | <source lang=python> | ||
Line 30: | Line 31: | ||
</source> | </source> | ||
== Writing / Outputting CSV == | |||
----- | ----- |
Revision as of 16:13, 29 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'}
Reading CSV as a dictionary...
#!/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)
Writing / Outputting CSV
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