Commit 3718fcde authored by George Lin's avatar George Lin

Initial commit

parents
from email import header
import git
import json
import os
import csv
import pandas
def get_input():
input_dict = {}
print("***** python CLI *****")
input_dict["is_local"] = input(
"Is the JSON file in your local directory? -[y/N] ")
input_dict["file_name"] = input("What is the filename: ")
if input_dict["is_local"].lower() == 'y':
input_dict["file_directory"] = input("Where is the file located: ")
else:
input_dict["file_directory"] = input("What is the git repo url: ")
return input_dict
def find_file(file_name, find_path):
for root, dirs, files in os.walk(find_path):
if file_name in files:
return os.path.join(root, file_name)
def output_analysis(file_dir):
df = pandas.read_csv(file_dir)
header = list(df.columns.values)
for i in range(0, len(header) - 1):
field_length = df[header[i]].astype(str).map(len)
result = df.loc[field_length.argmax(), header[i]]
if not str(result).replace('.', '', 1).isdigit():
print("{} : {}".format(header[i], result))
else:
print("{} : {}".format(header[i], df[header[i]].max()))
def python_CLI():
input_dict = get_input()
is_local = input_dict["is_local"] == 'y'
filename = input_dict["file_name"]
file_dir = input_dict["file_directory"]
data_file = None
data = None
header = None
try:
if is_local:
data_file = open(os.path.join(file_dir, filename))
else:
git.Git(os.getcwd()).clone(
file_dir)
data_file = open(find_file(filename, os.getcwd()))
data = json.load(data_file)
header = list(data[0].keys())
with open(".".join([filename.split('.')[0], "csv"]), 'w', encoding='UTF8') as f:
writer = csv.writer(f)
# write the header
writer.writerow(header)
# write the data
for item in data:
writer.writerow(list(item.values()))
output_analysis(".".join([filename.split('.')[0], "csv"]))
except:
print("Something is wrong, Check if URL is entered correctly.")
exit(1)
if __name__ == "__main__":
python_CLI()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment