-
Notifications
You must be signed in to change notification settings - Fork 1
/
stockReport.py
61 lines (47 loc) · 1.9 KB
/
stockReport.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
import json
def write_json(data, filename):
with open(filename, 'w') as addStock:
json.dump(data, addStock, indent=4)
def stock(element): # Return True if passed Element is Present else return False
with open('stock.json') as stockReport:
dataOnFile = json.load(stockReport)
for datas in dataOnFile["reportDetail"]:
if datas.get("stockName") == element:
return True
return False
def add(): # To add New Entry into the JSON File
stockName = input("Enter Your Stock Nmae: ")
stockNumber = str(input("Enter Your Stock Number: "))
stockPrice = str(input("Enter Your Stock Price: "))
try:
stockDetail = {
"stockName": stockName,
"stockNumber": stockNumber,
"stockPrice": stockPrice
}
with open('stock.json') as stockReport:
dataOnFile = json.load(stockReport)
temp = dataOnFile["reportDetail"]
temp.append(stockDetail)
write_json(dataOnFile, 'stock.json')
print("Data Saved !!!")
except:
stockDetail = {
"reportDetail": [
{
"stockName": stockName,
"stockNumber": stockNumber,
"stockPrice": stockPrice,
}
]}
write_json(stockDetail, 'stock.json')
print("Data Saved !!!")
def search(element): # Search for Data Based on FirstName or MobileNumber or LastName of the Entry
with open('stock.json') as stockReport:
dataOnFile = json.load(stockReport)
for datas in dataOnFile["reportDetail"]:
if element == datas.get("stockName") or element == datas.get("stockNumber") or element == datas.get("stockPrice"):
print(datas)
search("VEDANTA")
add()
#count(element)