Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pythoncli-hw
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
George Lin
pythoncli-hw
Commits
3718fcde
Commit
3718fcde
authored
Jun 03, 2022
by
George Lin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit
parents
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
90 additions
and
0 deletions
+90
-0
main.py
main.py
+90
-0
No files found.
main.py
0 → 100644
View file @
3718fcde
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
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment