Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, December 8, 2011

Python CSV Parsing

If you haven't realized already, I live and breathe in a command-line world. That's not even just talking about my work-related things; even when helping out helping out someone who is running Windows I will do majority of the things I need to do in the cmd prompt. Anyhow, I'm starting to ramble...

I also sporadically get some data sent to me in csv (or I will convert it to csv format). Now, if you have done this enough times, you'll obviously want to have some sort of csv parser. And what do you know, voila! I've got a simple one here for all of my command-line junkies out there!

csvreader.py is short enough to be available as a short gist on github. --My first activity on github by the way!

I'm sure you'll all find a nice way to use that. If you were curious what I ended up doing, well here's a generic code snippet: for f in `python csvreader.py tmp/b.20111208_1042.csv 0`; do echo $f; php postInfo.php /mnt/$f; done;

Cheers!

Wednesday, August 3, 2011

Python: Reading and Running Commands From A File

So I recently came across a broken process that needed some manual re-running. I was able to search through the log files and then see what part needed to be re-run. I ended up with a new data file (that I modified) to be a list of each command that failed for each iteration. So, the file looked something like this:
/path/to/program -option x -site a
/path/to/program -option x -site b
...
/path/to/program -option x -site z



Using a bash for line in `cat command.file`; do $line; done would not work since I would end up with a
-bash: -option: command not found
-bash: x: command not found
-bash: -site: command not found
error. So, here's some quick Python that can get the job done:

import os

f = open('/tmp/tmp-fix.commands')
for line in f:
    print line.rstrip()
    os.system(line.rstrip())


Yay for saving me some time! Cheers!

Tuesday, July 26, 2011

Python: Getting Line Counts of Files and Reading Directories

I have a bunch of little scripts to help out with day-to-day tasks (don't we all?). For some reason, (probably because I've been busy) I never posted anything tagged with "python". Whoa! Sharing is caring, so here it is.


import sys
import os

def lines_in_file(f):
    return len(open(f).readlines())

files = os.listdir(sys.argv[1])
totalcount = 0
for f in files:
    totalcount += lines_in_file(sys.argv[1] + f)

print totalcount

Why would this be helpful?

Well, it may or may not to you, but maybe you're just looking for a python example on how to get a line count of a file:
def lines_in_file(f):
    return len(open(f).readlines())

Or maybe, you're just looking for a python example to list files in a directory:
files = os.listdir(sys.argv[1])
for f in files:
    print f ## modified from the original script, to show you an example

How I Use It In Case You Were Curious...

So, we have this one cron job that parses through a directory that contains CSV files. So essentially, each line is a python run. Or I can also think of it as, X number of accounts are participating in Job Y.

That's it! Cheers!