Skip to content

Commit b3d4b7f

Browse files
authored
added working with json program
1 parent 7034e91 commit b3d4b7f

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"aspire_7": {"cpu": "i5-9300h", "gpu": "gtx 1650 4gb ddr5", "ram": "8gb", "refresh rate": "60hz", "weight": "2.2kg", "wifi ": 6}, "nitro_5": {"cpu": "r5-3500h", "gpu": "gtx 1650 4gb ddr5", "ram": "8gb", "refresh rate": "120hz", "weight": "2.5kg", "wifi ": 5}}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
2+
Type "help", "copyright", "credits" or "license()" for more information.
3+
>>> f=open("E:\\Python\\Beginner_Level\\working with json\\file.txt",'r')
4+
>>> s=f.read()
5+
>>> s
6+
'{"aspire_7": {"cpu": "i5-9300h", "gpu": "gtx 1650 4gb ddr5", "ram": "8gb", "refresh rate": "60hz", "weight": "2.2kg", "wifi ": 6}, "nitro_5": {"cpu": "r5-3500h", "gpu": "gtx 1650 4gb ddr5", "ram": "8gb", "refresh rate": "120hz", "weight": "2.5kg", "wifi ": 5}}'
7+
>>> # convert from json to dictionary
8+
>>> import json
9+
>>> laptop = json.loads(s)
10+
>>> laptop
11+
{'aspire_7': {'cpu': 'i5-9300h', 'gpu': 'gtx 1650 4gb ddr5', 'ram': '8gb', 'refresh rate': '60hz', 'weight': '2.2kg', 'wifi ': 6}, 'nitro_5': {'cpu': 'r5-3500h', 'gpu': 'gtx 1650 4gb ddr5', 'ram': '8gb', 'refresh rate': '120hz', 'weight': '2.5kg', 'wifi ': 5}}
12+
>>> # difference above 2 output is 1st one is json/string and 2nd one is dictionary
13+
>>> type(laptop)
14+
<class 'dict'>
15+
>>> type(s)
16+
<class 'str'>
17+
>>> laptop['aspire_7']['gpu']
18+
'gtx 1650 4gb ddr5'
19+
>>> laptop['nitro_5']['']
20+
Traceback (most recent call last):
21+
File "<pyshell#11>", line 1, in <module>
22+
laptop['nitro_5']['']
23+
KeyError: ''
24+
>>> # to print weight of laptop in laptop dictionary
25+
>>> for weight in laptop:
26+
print(laptop['weight'])
27+
28+
29+
Traceback (most recent call last):
30+
File "<pyshell#15>", line 2, in <module>
31+
print(laptop['weight'])
32+
KeyError: 'weight'
33+
>>> for weight in laptop:
34+
print(laptop[weight])
35+
36+
37+
{'cpu': 'i5-9300h', 'gpu': 'gtx 1650 4gb ddr5', 'ram': '8gb', 'refresh rate': '60hz', 'weight': '2.2kg', 'wifi ': 6}
38+
{'cpu': 'r5-3500h', 'gpu': 'gtx 1650 4gb ddr5', 'ram': '8gb', 'refresh rate': '120hz', 'weight': '2.5kg', 'wifi ': 5}
39+
>>>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
laptop = {}
2+
laptop['aspire_7'] = {
3+
'cpu' : 'i5-9300h',
4+
'gpu' : 'gtx 1650 4gb ddr5',
5+
'ram' : '8gb',
6+
'refresh rate' : '60hz',
7+
'weight': '2.2kg',
8+
'wifi ' : 6
9+
}
10+
laptop['nitro_5'] = {
11+
'cpu' : 'r5-3500h',
12+
'gpu' : 'gtx 1650 4gb ddr5',
13+
'ram' : '8gb',
14+
'refresh rate' : '120hz',
15+
'weight': '2.5kg',
16+
'wifi ' : 5
17+
}
18+
19+
import json
20+
s = json.dumps(laptop) # convert dictionary to JSON format i.e is exchangeable
21+
print(s)
22+
23+
# to store json in a file
24+
25+
with open("E:\\Python\Beginner_Level\\working with json\\file.txt",'w') as f:
26+
f.write(s)

0 commit comments

Comments
 (0)