Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Saturday, January 30, 2016

Reading text out of PDF bank statements: takes some pain out of tax time!

I was working on my wife's business taxes today for Washington State. Our state has a simple and simply not very fair tax: the B&O (Business & Occupation) tax. But that's not the issue today; the issue is my grinding my teeth about retyping stuff from a bunch of PDF bank statements.


I was starting on this while sighing and then thought, hey, there should be something!

And indeed good old Ghostscript came to the rescue!

I already had this program on my 2013 Macintosh, but it was an older version that didn't have a necessary device, but I downloaded and compiled the latest Ghostscript 9.18 and was able to run:

#!/usr/bin/python

import glob
import os

for f in glob.glob('*.pdf'):

    os.system('gs -sDEVICE=txtwrite -o %s.txt %s' % (f,f))

This program takes all the PDF files in the current directory and converts them to plain text!

Then to get the data out of these files that I was looking for there was a lovely Unix-y command line pipe string to do the trick:

grep Rain *.txt | awk '{ print $3 }' | ~/bin/add.py 

I was looking for the transactions starting with 'Rain' and the numbers were in the third field, and the final program in the chain is a simple adder:

#!/usr/bin/python
import sys

n = 0.0
for line in sys.stdin:
    nline = float(line.rstrip())
    n += nline

print n

Hope this is helpful!

Sunday, June 14, 2015

Code: An Essay

I read this Bloomberg BusinessWeek article on code this week and it was excellent for anybody


who's not quite sure about code and very enjoyable for me (a computer geek with experience similar to that of the author.)

38000 words: make sure you have time for it! Enjoy ...

Tuesday, July 22, 2014

C'mon, can't I leave spaces in my credit card number? It's 2014!

Probably you've had this experience ... filling out a purchase form to buy something online and entering the credit card number exactly as it appears on the card, say:

4444 5555 6666 7777

You click Submit and you get back (quickly via Javascript or less quickly with a page refresh) something like:


Why the hell should I have to remove the spaces in the form in this day and age?

To be fair, a few companies get this right, Amazon among them. Others refuse to let you put in a space some Javascript trick as you're typing. This is better than an error message, but not much. Finally, some remove spaces after you hit submit but before you finalize your purchase on another page. This is silly but tolerable.

Let me say this clearly: the display of the credit card number does not need to look exactly like the credit card number that's transmitted to the back end system.

Humans aren't good at parsing a 16-digit number; we need spaces or dashes to break up the pieces of the number.

Web geeks must just be missing the code to do this, so I will provide some examples here in hope of putting this issue to rest for all of us for the future.

Javascript:

var card = '4444 5555 6666 7777';
var cnostr = card.replace(/\s+/g, '');
alert('card b4:' + card);
alert('card after:' + cnostr);

Python:

import re
card = '4444 5555 6666 7777'
(cnostr,ign) = re.subn('[\s+]','',card)
print 'card = %s' % card
print 'cnostr = %s' % cnostr

Other languages are left as an exercise for the reader.

How about this, folks? Please send a link to this page to the purveyor of any web form that doesn't allow you to put spaces in a credit card number. We can start to get this fixed!