Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit35e2998

Browse files
committed
added working with json files tutorial
1 parenta951f9b commit35e2998

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
9494
-[How to List all Files and Directories in FTP Server using Python](https://www.thepythoncode.com/article/list-files-and-directories-in-ftp-server-in-python). ([code](python-standard-library/listing-files-in-ftp-server))
9595
-[How to Read Emails in Python](https://www.thepythoncode.com/article/reading-emails-in-python). ([code](python-standard-library/reading-email-messages))
9696
-[How to Download and Upload Files in FTP Server using Python](https://www.thepythoncode.com/article/download-and-upload-files-in-ftp-server-using-python). ([code](python-standard-library/download-and-upload-files-in-ftp))
97+
-[How to Work with JSON Files in Python](https://www.thepythoncode.com/article/working-with-json-files-in-python). ([code](python-standard-library/working-with-json))
9798

9899
-###[Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
99100
-[How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
@@ -103,7 +104,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
103104
-[How to Use Google Drive API in Python](https://www.thepythoncode.com/article/using-google-drive--api-in-python). ([code](general/using-google-drive-api))
104105
-[How to Translate Text in Python](https://www.thepythoncode.com/article/translate-text-in-python). ([code](general/using-google-translate-api))
105106

106-
-###Database
107+
-###[Database](https://www.thepythoncode.com/topic/using-databases-in-python)
107108
-[How to Use MySQL Database in Python](https://www.thepythoncode.com/article/using-mysql-database-in-python). ([code](database/mysql-connector))
108109
-[How to Connect to a Remote MySQL Database in Python](https://www.thepythoncode.com/article/connect-to-a-remote-mysql-server-in-python). ([code](database/connect-to-remote-mysql-server))
109110
-[How to Use MongoDB Database in Python](https://www.thepythoncode.com/article/introduction-to-mongodb-in-python). ([code](database/mongodb-client))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#[How to Work with JSON Files in Python](https://www.thepythoncode.com/article/working-with-json-files-in-python)
2+
To run`example.py`, you have to install`requests` library:
3+
-`pip3 install -r requirements.txt`
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
importrequests
2+
importjson
3+
4+
5+
# make API request and parse JSON automatically
6+
data=requests.get("https://jsonplaceholder.typicode.com/users").json()
7+
# save all data in a single JSON file
8+
file_name="user_data.json"
9+
withopen(file_name,"w")asf:
10+
json.dump(data,f,indent=4)
11+
print(file_name,"saved successfully!")
12+
13+
# or you can save each entry into a file
14+
foruserindata:
15+
# iterate over `data` list
16+
file_name=f"user_{user['id']}.json"
17+
withopen(file_name,"w")asf:
18+
json.dump(user,f,indent=4)
19+
print(file_name,"saved successfully!")
20+
21+
22+
# load 2nd user for instance
23+
file_name="user_2.json"
24+
withopen(file_name)asf:
25+
user_data=json.load(f)
26+
27+
print(user_data)
28+
print("Username:",user_data["username"])
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
importjson
2+
3+
# read a JSON file
4+
# 1st option
5+
file_name="data1.json"
6+
withopen(file_name)asf:
7+
data=json.load(f)
8+
9+
print(data)
10+
# 2nd option
11+
file_name="data2.json"
12+
withopen(file_name)asf:
13+
data=json.loads(f.read())
14+
15+
print(data)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
importjson
2+
3+
# example dictionary to save as JSON
4+
data= {
5+
"first_name":"John",
6+
"last_name":"Doe",
7+
"email":"john@doe.com",
8+
"salary":1499.9,# just to demonstrate we can use floats as well
9+
"age":17,
10+
"is_real":False,# also booleans!
11+
"titles": ["The Unknown","Anonymous"]# also lists!
12+
}
13+
14+
# save JSON file
15+
# 1st option
16+
withopen("data1.json","w")asf:
17+
json.dump(data,f)
18+
19+
# 2nd option
20+
withopen("data2.json","w")asf:
21+
f.write(json.dumps(data,indent=4))
22+
23+
24+
unicode_data= {
25+
"first_name":"أحمد",
26+
"last_name":"علي"
27+
}
28+
29+
withopen("data_unicode.json","w",encoding="utf-8")asf:
30+
json.dump(unicode_data,f,ensure_ascii=False)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp