CSV: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
See also: [[csvkit]]
https://docs.python.org/3.7/library/csv.html
'''Convert a CSV file into a python list of dictionaries for each row'''  
'''Convert a CSV file into a python list of dictionaries for each row'''  


Line 16: Line 20:
</pre>
</pre>


== Reading CSV as a dictionary...==


<source lang=python>
<source lang=python>
Line 29: Line 34:
     print(entry)
     print(entry)


</source>
== Writing / Outputting CSV ==
Example from the python documentation:
<source lang="python">
import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
</source>
</source>



Latest revision as of 15:18, 29 May 2020

See also: csvkit

https://docs.python.org/3.7/library/csv.html

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

Example from the python documentation:

import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])


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