Among other things, I like python. I quite like the Talk Python to Me podcast, done by Michael Kennedy. He is, among other things, a python trainer. Among his online python training videos is one on parsing csv (comma separated value) files. His example didn't actually use the python csv module, so I whipped up a couple of examples that used csv.reader and csv.DictReader.
These use python 3.4.
===================
csv.reader example:
#!c:\\anaconda3\\python.exe
'''taken from https://www.youtube.com/watch?v=qajONCIvhEc
https://support.spatialkey.com/spatialkey-sample-csv-data/
http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv
'''
import csv
import os
def main():
print("Hello File I/O minicast using csv.reader")
filename = os.path.abspath(os.path.join("data", "Sacramentorealestatetransactions.csv"))
print(filename)
with open(filename, 'r') as csvfile:
fin = csv.reader(csvfile, delimiter=',')
header = next(fin)
print(header)
# ['street', 'city', 'zip', 'state', 'beds', 'baths', 'sq__ft', 'type', 'sale_date', 'price', 'latitude', 'longitude']
entries = []
for line in fin:
# lines look like
# ['3526 HIGH ST', 'SACRAMENTO', '95838', 'CA', '2', '1', '836', 'Residential', 'Wed May 21 00:00:00 EDT 2008', '59222', '38.631913', '-121.434879']
row = dict()
for i, h in enumerate(header):
row[h] = line[i]
entries.append(row)
entries.sort(key= lambda r: -1*int(r['price']))
# Entries are a list of dicts. Each line looks like:
# {'street': '9401 BARREL RACER CT', 'state': 'CA', 'sq__ft': '4400', 'city': 'WILTON', 'beds': '4', 'baths': '3', 'sale_date': 'Fri May 16 00:00:00 EDT 2008', 'price': '884790', 'longitude': '-121.194858', 'latitude': '38.415298', 'zip': '95693', 'type': 'Residential'}
for e in entries[:5]:
print("{0} beds, {1} baths sold for ${2:,}".format(
e['beds'], e['baths'], int(e['price'])
))
if _name_ == '__main__':
main()
=======================
csv.DictReader example:
#!c:\\anaconda3\\python.exe
'''taken from https://www.youtube.com/watch?v=qajONCIvhEc
https://support.spatialkey.com/spatialkey-sample-csv-data/
http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv
'''
import csv
import os
def main():
print("Hello File I/O minicast using csv.DictReader")
filename = os.path.abspath(os.path.join("data", "Sacramentorealestatetransactions.csv"))
print(filename)
with open(filename, 'r') as csvfile:
fin = csv.DictReader(csvfile, delimiter=',')
print(fin.fieldnames)
# ['street', 'city', 'zip', 'state', 'beds', 'baths', 'sq__ft', 'type', 'sale_date', 'price', 'latitude', 'longitude']
entries = list(fin)
# lines/rows in fin look like
# {'street': '3882 YELLOWSTONE LN', 'state': 'CA', 'sq__ft': '1362', 'city': 'EL DORADO HILLS', 'beds': '3', 'baths': '2', 'sale_date': 'Thu May 15 00:00:00 EDT 2008', 'price': '235738', 'longitude': '-121.075915', 'latitude': '38.655245', 'zip': '95762', 'type': 'Residential'}
entries.sort(key= lambda r: -1*int(r['price']))
for e in entries[:5]:
print("{0} beds, {1} baths sold for ${2:,}".format(
e['beds'], e['baths'], int(e['price'])
))
if _name_ == '__main__':
main()
Thursday, September 3, 2015
Thursday, March 26, 2015
Fixing Linux Firefox DEE (mail.mil)
It's a constant battle to be able to receive my Defense Enterprise Email on linux. (Heck, it's not that far off on Windows, either.) I had it working, then it stopped, and militarycac.com came to the rescue. It turns out that you need to renegotiate the SSL session while authenticating, which is a security hole. https://militarycac.com/firefox.htm had the right info for me:
You'll need to enable SSL renegotiation, do this by pointing your browser to about:config. After confirming that you know what you are doing, you need to start typing in:I didn't even need to restart firefox! Now I can be in linux more often, and not just when I think I won't need to check email. DoD is moving to an all-in-one outlook web access (OWA) client, and it is much finickier than normal CAC (smart card) enabled web sites.
security.ssl.allow_unrestricted_renego_everywhere__temporarily_available_prefset it to true (by double clicking it). After this you should be able to access the site.
Monday, March 16, 2015
Renaming files in Windows
Sometimes the small things are the most helpful. I needed to rename a couple dozen jpg files to "...small.jpg". Through the joys of Internet searching, I pretty quickly came to a solution from http://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/ using Windows PowerShell. So with a quick:
PS > dir | Rename-Item -NewName {$_.name -replace ".jpg","Small.jpg"}
FirstPicture.jpg became FirstPictureSmall.jpg. I hadn't installed The Gimp yet on this laptop, but I found through a YouTube video (http://www.youtube.com/watch?v=19GbI6oFrW0) that you can use Right Click > Send To > Mail Recipient, and Outlook (assuming it's installed) will offer to change the size for you.
PS > dir | Rename-Item -NewName {$_.name -replace ".jpg","Small.jpg"}
FirstPicture.jpg became FirstPictureSmall.jpg. I hadn't installed The Gimp yet on this laptop, but I found through a YouTube video (http://www.youtube.com/watch?v=19GbI6oFrW0) that you can use Right Click > Send To > Mail Recipient, and Outlook (assuming it's installed) will offer to change the size for you.
Subscribe to:
Posts (Atom)