Created
June 20, 2011 19:42
-
-
Save pbdeuchler/1036388 to your computer and use it in GitHub Desktop.
Quick python script for parsing CSV files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python -tt | |
#Didn't you hear? The bird is the word! | |
#@version: 0.1 | |
import sys | |
def main(filename): | |
f = open(filename, "r") | |
f2 = open("log.txt", "w") | |
count = 0 | |
for line in f: | |
result = parse(line) | |
if not result == "null" and " 0" not in result: | |
result = result + '\n' | |
f2.write(result) | |
count = count + 1 | |
count = str(count) | |
message = 'Number of good lines found: %s' % count | |
f2.write(message) | |
f.close() | |
f2.close() | |
def parse(line): | |
array = line.split(',') | |
try: | |
if array[11] is 0: | |
final = "null" | |
return final | |
except: | |
final = "null" | |
return final | |
duration = int(array[7]) * 1000 | |
duration = str(duration) | |
called = clean(array[8]) | |
caller = clean(array[9]) | |
if " " in duration: | |
final = "null" | |
return final | |
if called is "null" or caller is "null": | |
final = "null" | |
return final | |
else: | |
#final = called + "," + caller + "," + duration | |
final = '%1s,%2s,%3s' % (called, caller, duration) | |
final = str(final) | |
return final | |
def clean(number): | |
if not number: | |
number = "null" | |
return number | |
if len(number) < 9: | |
number = "null" | |
return number | |
if number[0:4] == "tel:": | |
number = number[4:] | |
if number[0:5] == "sip:": | |
number = number[8:18] | |
if number[0:2] == "+1": | |
number = number[2:] | |
if number[0] == 1: | |
number = number[1:] | |
if not len(number) == 10: | |
number = "null" | |
return number | |
if number[0:3] == "700": | |
number = "null" | |
number = str(number) | |
return number | |
if __name__ == '__main__': | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment