May 23, 2007 10:21
Today, I needed to get some information out of my Amazon AWS usage report. (Bandwidth usage, in this case.) I wanted pretty output in gigs (usage is measured and reported in bytes) and I didn't want any of the extraneous stuff. Since AWS helpfully provides a CSV, I wrote a cunning little Python one-liner to extract it:
python -c 'print "\n".join([str(int(line.split(",")[-1].strip())/(1024.0**3)) for line in file("/tmp/report.csv").readlines()[1:]])'
You need to read this from the inside out, since it's a list comprehension.
First, it reads all the lines from the csv file and throws out the first (because I don't want the header). Then, it splits each line at the commas and only looks at the last. This is converted to an integer (stripping off whitespace first), divided by the number of bytes in a gig and turned back into a string. These processed lines are collected by the list comprehension, have newlines stuck in between them to make one long string and printed.
0.472050191835
1.29025535937
2.04500017408
2.04324277025
1.82879307121
2.03375558369
0.681352665648
Yay Python! (The Perl people could probably do it in fewer characters, but it would look more like line-noise.)
geekery,
programming,
python