Skip to content

Commit 7ecc7c4

Browse files
lpozobzaczynski
andauthored
Sample code for: Python 3.15 Preview: UTF-8 by Default (#797)
* Sample code for: Python 3.15 Preview: UTF-8 by Default * Rename example files descriptively and print entry count in app_v2 * Add data files read by the sample scripts Ship data.json, legacy.csv, and notes.txt so every script runs on first try instead of raising FileNotFoundError. Also align the os_encoding.py comment with the tutorial's lowercase "UTF-8 mode". * Keep app.py parsing JSON after adding the explicit encoding app_v2.py read the file as raw text, so len(data) counted characters and printed "Loaded 29 entries". Restore json.load() so the only difference from app_v1.py is the encoding argument, matching the tutorial. * Parse data.json with json.load() in explicit_encoding.py Keeps data.json handling consistent with app_v1.py and app_v2.py across the tutorial's examples. legacy.csv stays a raw read, since the point there is the locale sentinel rather than parsing. --------- Co-authored-by: Bartosz Zaczyński <bartosz.zaczynski@gmail.com>
1 parent b474a7c commit 7ecc7c4

12 files changed

Lines changed: 46 additions & 0 deletions

File tree

python315-utf8-default/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python 3.15 Preview: UTF-8 by Default
2+
3+
This folder provides the code examples for the Real Python tutorial [Python 3.15 Preview: UTF-8 by Default](https://realpython.com/python315-utf8-default/)

python315-utf8-default/app_v1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import json
2+
3+
with open("data.json") as f:
4+
data = json.load(f)
5+
6+
print(f"Loaded {len(data)} entries")

python315-utf8-default/app_v2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import json
2+
3+
from constants import ENCODING
4+
5+
with open("data.json", encoding=ENCODING) as f:
6+
data = json.load(f)
7+
8+
print(f"Loaded {len(data)} entries")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ENCODING = "utf-8" # Single source of truth for text I/O

python315-utf8-default/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"café": "", "naïve": true}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import json
2+
3+
with open("data.json", encoding="utf-8") as f: # Portable and unambiguous
4+
data = json.load(f)
5+
6+
with open("legacy.csv", encoding="locale") as f: # Intentional locale use
7+
legacy = f.read()

python315-utf8-default/legacy.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id,name,quantity
2+
1,espresso,12
3+
2,americano,8
4+
3,macchiato,5
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
with open("legacy.csv", encoding="locale") as f:
2+
legacy = f.read()

python315-utf8-default/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import textlib
2+
3+
textlib.read_text("notes.txt")

python315-utf8-default/notes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remember to pass an explicit encoding to every text-mode open() call.

0 commit comments

Comments
 (0)