-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectEntitiesData.py
executable file
·79 lines (68 loc) · 1.91 KB
/
CollectEntitiesData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import json
import sys
import csv
import os
root = {}
root["Person"] = []
def input_from_file():
input_file = sys.argv[1]
csv.register_dialect('myDialect', delimiter = ',',quoting=csv.QUOTE_ALL,skipinitialspace=True)
declared_headers = []
csv_rows = []
with open(input_file,'r') as csvFile:
reader = csv.DictReader(csvFile)
root["Person"] = [row for row in reader]
for i in root["Person"]:
i.pop("nationality", None)
print(json.dumps(root, indent=4))
def user_input():
print("Proceeding User input ..")
while True:
# User data from input
first_name = input("What's your first name?")
last_name = input("What's your last name?")
print("Nice to meet you " + first_name + last_name + "!")
while True:
try:
age = int(input("Your age? "))
except ValueError:
print("Sorry we didnt understand")
continue
if age < 0:
print("Sorry your response must not be negative ")
continue
else:
#Value is good
break
print("So, you are already %d years old, " % (age) + first_name + "!")
favourite_color = input("what is your favourite color ?")
print(favourite_color+" is nice!")
Nationality = input("what is your Nationality?")
# User data to json dump
data = {}
data['first_name'] = first_name
data['last_name'] = last_name
data['age'] = age
data['favourite_color'] = favourite_color
root["Person"].append(data)
Furtherdata = input(" Want to feed more data ? yes/no ")
if Furtherdata == "yes":
continue
else:
break
print(json.dumps(root, sort_keys=True, indent=4))
def main():
if len(sys.argv) == 2:
try:
fh = open(sys.argv[1],"r")
input_from_file()
except FileNotFoundError:
print("Input file is not available!! please check")
elif len(sys.argv) > 2:
print("Unknown no.of arguments, please pass only one input file ")
print("e.g., python3 "+sys.argv[0]+" <Input file>")
exit()
else:
user_input()
if __name__ == "__main__":
main()