Created
January 17, 2019 20:53
-
-
Save craigderington/cc2a92a0a110ecf5ee1575a310cb2569 to your computer and use it in GitHub Desktop.
This file contains 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/env python | |
import argparse | |
import csv | |
import json | |
from types import * | |
def main(): | |
parser = argparse.ArgumentParser( | |
description='Convert json file to csv' | |
) | |
parser.add_argument( | |
'-i', | |
'--input_file', | |
dest='input_file', | |
default=None, | |
required=True, | |
help='Source json file (mandatory)' | |
) | |
parser.add_argument( | |
'-o', | |
'--output_file', | |
dest='output_file', | |
default=None, | |
required=True, | |
help='Destination csv file (mandatory)' | |
) | |
args = parser.parse_args() | |
input_file = args.input_file | |
output_file = args.output_file | |
json_data = [] | |
data = None | |
write_header = True | |
field_names = ["created_date", "ip", "dealer", "product", "job_number", "client_id", "vendor"] | |
with open('data/campaign_data.txt', 'r') as json_file: | |
json_data = json_file.readlines()[:1280] | |
try: | |
data = json_data | |
except Exception as e: | |
raise e | |
with open('data/campaign_data.csv', 'wb') as csv_file: | |
w = csv.writer(csv_file, delimiter=',') | |
if write_header: | |
w.writerow(field_names) | |
write_header = False | |
clean_ip = None | |
visitor_date = None | |
for item in data: | |
if 'created_date' in item: | |
created_date = item.split(':') | |
visitor_date = created_date[1:] | |
visitor_date = str(visitor_date[:1]) | |
visitor_date = visitor_date.strip('\n') | |
elif 'ip' in item: | |
ip = item.split(':') | |
clean_ip = str(ip[1:]) | |
clean_ip = clean_ip.strip('\n') | |
data_row = visitor_date, clean_ip, "United Health Care", "ip_Tracker_v.03", "32746", "UHC10004", "PDS" | |
w.writerow(data_row) | |
print(data_row) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment