Skip to content

Commit 6c55685

Browse files
committed
add code for bytes to strings article
1 parent 961f910 commit 6c55685

File tree

6 files changed

+29
-0
lines changed

6 files changed

+29
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from urllib.request import urlopen
2+
3+
url = "https://example.com/"
4+
5+
with urlopen(url) as response:
6+
raw_bytes: bytes = response.read()
7+
8+
print(f"Bytes: {raw_bytes[:100]}\n")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from urllib.request import urlopen
2+
3+
url = "https://example.com/"
4+
5+
with urlopen(url) as response:
6+
raw_bytes: bytes = response.read()
7+
8+
print(f"Bytes: {raw_bytes[:100]}\n")
9+
10+
string_format = raw_bytes[:100].decode()
11+
12+
print(f"String format: {string_format}\n")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
raw_bytes = b"These are some interesting bytes"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hex_representation = b"\xff\xfe\xfa"
2+
print(hex_representation.decode("utf-8", errors="replace"))

python-bytes-to-strings/test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
raw_bytes = b"These are some interesting bytes"
2+
raw_bytes.replace("y", "o")
3+
4+
print(raw_bytes)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hex_representation = b"\xff\xfe\xfa"
2+
print(hex_representation.decode("utf-8"))

0 commit comments

Comments
 (0)