Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
python3-cookbook/basic/mystring/str_to_bytes.py at master · pythoningLearning/python3-cookbook · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
pythoningLearning
/
python3-cookbook
Public
forked from
yidao620c/python3-cookbook
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
python3-cookbook
/
basic
/
mystring
/
str_to_bytes.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
68 lines (59 loc) · 3.45 KB
master
Breadcrumbs
python3-cookbook
/
basic
/
mystring
/
str_to_bytes.py
Copy path
Top
File metadata and controls
Code
Blame
68 lines (59 loc) · 3.45 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------------------------------------
Function:
【整理】Python中字符编码的总结和对比
Python 3.x中,直接输出的字符串(被单引号或双引号括起来的),就已经是Unicode类型的str了。
当然,有一些前提:
1. Python文件开始已经声明对应的编码
2. Python文件本身的确是使用该编码保存的
3. 两者的编码类型要一样(比如都是UTF-8或者都是GBK等)
这样Python解析器,才能正确的把你所输出字符串,解析为对应的unicode的str
Author: Xiong Neng
Verison: 2014-09-25
-------------------------------------------------------------------------------
"""
def
str_to_bytes
():
"""Demo Python 3.x (unicode) str to bytes
"""
zhcn_unicode
=
"""
1.此处的,Python 3.x中,默认字符串的写法,就已经是unicode类型的字符串了。
2.当然,还是有一点前提的,那就是:
(1)此处python文件所指定的编码类型
(2)要和你当前python文件实际所采用的编码类型,要匹配和一致,
即此处,两者均是UTF-8,所以,Python解析器,才能正确的将我们此处所输入的UTF-8的中文字符,
正确地解码为对应的Unicode字符串的;
3.接下来将要演示的是,打印对于的此处字符的类型;
然后再直接输出显示到windows的GBK编码的cmd中
"""
print
(
"type(zhcn_unicode)="
,
type
(
zhcn_unicode
))
# type(zhcn_unicode)= <class 'str'>
print
(
zhcn_unicode
)
zhcn_gbk_bytes
=
zhcn_unicode
.
encode
(
"GBK"
)
# print("You should see these zh-CN bytes in windows cmd normally,"
# " which begin with b preffix: zhcnGbkBytes=%s" % (zhcn_gbk_bytes))
print
(
'中'
.
encode
(
'UTF-8'
))
# UTF-8的中文3个字节,输出 b'\xe4\xb8\xad'
# You should see these zh-CN bytes in windows cmd normally,
# which begin with b preffix:
# zhcnGbkBytes=b'1.\xb4\xcb\xb4\xa6\xb5 ...... \xc2\xeb\xb5\xc4cmd\xd6\xd0'
def
bytes_to_str
():
"""Demo Python 3.x bytes to (unicode) str
"""
#此处的bytes,只能接受ASCII字符
#想要输入非ASCII的字符,则只能通过\xYY的十六进制方式输入,其中YY为对应的16进制的值
#此处,我是已经在别处,通过把对应的中文:
#"1.Python 3.x中,给字符串前面添加字母b,表示是bytes的字符串;
# 2.此处之所以可以实现,接下来的,Python解析器,可以正确的将bytes解码为Unicode的str,那是因为
# (1)此处python文件所指定的编码类型
# (2)要和你当前python文件实际所采用的编码类型,是一致的,都是UTF-8;
# 3.接下来将要演示的是,将此bytes字符串,解码为Unicode的str,
# 然后在此处的终端,windows的默认编码为GBK的cmd中显示出来;";
#解析为UTF-8的bytes了,所以下面你看到的是,解析后的,一堆bytes
zhcnBytes
=
b"1.
\xe6
\xad
\xa4
\xe5
\xa4
\x84
\xe7
\x9a
\x84
\xef
\xbc
\x8c
Python 3.x"
print
(
"type(zhcnBytes)="
,
type
(
zhcnBytes
))
# type(zhcnBytes)= <class 'bytes'>
zhcnUnicodeStr
=
zhcnBytes
.
decode
(
"UTF-8"
)
print
(
"zh-CN unicode str in windows cmd normally: zhcnUnicodeStr=%s"
%
(
zhcnUnicodeStr
))
# zh-CN unicode str in windows cmd normally: zhcnUnicodeStr=
# 1.此处的,Python 3.x中 ...... 然后再直接输出显示到windows的GBK编码的cmd中
if
__name__
==
"__main__"
:
str_to_bytes
()
You can’t perform that action at this time.