forked from mCodingLLC/VideosSampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr_vs_bytes.py
126 lines (96 loc) · 2.78 KB
/
str_vs_bytes.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def converting_between_str_bytes():
s = "abc"
b = s.encode("utf-8")
print(s)
print(b)
b = b"abc"
s = b.decode("utf-8")
print(s)
print(b)
def str_bytes_syntax():
s = "Hello from str"
b = b"Hello from bytes"
print(s)
print(b)
s = 'Hello from str'
b = b'Hello from bytes'
print(s)
print(b)
s = """Hello from str"""
b = b'''Hello from bytes'''
print(s)
print(b)
s = "Hello from str\n"
b = b"Hello from bytes\n"
print(s)
print(b)
s = r"Hello from str\n"
b = rb"Hello from bytes\n"
print(s)
print(b)
s = f"Hello from str in {__name__}"
# b = fb"Hello from bytes in {__name__}" # Error
print(s)
def str_bytes_functions():
str_functions = set(dir(str))
bytes_functions = set(dir(bytes))
print("str and bytes common functions")
for func in sorted(str_functions & bytes_functions):
print(func)
print()
print("str but not bytes functions")
for func in sorted(str_functions - bytes_functions):
print(func)
print()
print("bytes but not str functions")
for func in sorted(bytes_functions - str_functions):
print(func)
print()
def writing_to_a_file():
with open("file.txt", "w") as fp:
fp.write("some str")
# fp.write(b"some bytes") # Error
fp.write(b"some bytes".decode()) # OK
with open("file.txt", "wb") as fp:
# fp.write("some str") # Error
fp.write("some str".encode()) # OK
fp.write(b"some bytes")
def smiley():
s = "😊"
s = "\U0001F60A"
# b = b"😊"
b = s.encode("utf-8")
print(b)
print(f"{len(s)=}")
print(f"{len(b)=}")
print(list(b))
print(int.from_bytes(b, byteorder="little"))
print(int.from_bytes(b, byteorder="big"))
print(b.decode("utf-8"))
def example_encode_decode_wrong_encoding():
# on computer with utf-8 encoding
s = "Hello, world! 😊"
with open("data.txt", "w", encoding="utf-8") as fp:
fp.write(s)
# OK
with open("data.txt", encoding="utf-8") as fp:
print(fp.read())
# Garbage on the end
with open("data.txt", encoding="iso-8859-1") as fp:
print(fp.read())
# Error!
# with open("data.txt", encoding="Windows-1251") as fp:
# print(fp.read())
# Note: utf-8 is scheduled to become the default in Python 3.15
# https://peps.python.org/pep-0686/
# Note: in Python 3.10+ run with -X warn_default_encoding to get a warning
# if you forget to specify encoding
# Note: in Python 3.7+ run with -X utf8 to assume utf-8 if not specified
def main():
# converting_between_str_bytes()
# str_bytes_syntax()
# str_bytes_functions()
# smiley()
example_encode_decode_wrong_encoding()
if __name__ == '__main__':
main()