python-3.x - 在 Python 3 中,在 encode('utf-8') 之后,可变长度从 2 变为 4
问题描述
我正在将 Python 2 代码转换为 Python 3,并且需要使用 encode() 将 str 转换为字节。
让 str 变量的名称为 s1。
# -*- coding: utf-8 -*-
使用*.py 文件的良性设置 unicode 。
在 Python 2 中: type(s1)= <type 'str'>
, len(s1)=2;
在 Python 3 中: type(s1)= <class 'str'>
, len(s1)=2; type(s1.encode())= <class 'bytes'>
,但 len(s1.encode())=4。
此长度更改导致错误:unpack('H', s1.encode())
,错误:解包需要 2 个字节的缓冲区
解决方案
推荐阅读
- c# - C# 多列单列总和作为 DataRow
- java - 如何将我用paintComponent 绘制并用鼠标单击的形状带到前面?
- python - 处理 pandas read_sql() 中可选块大小的函数
- heroku - 如何使用 blazegraph 托管我自己的 Triplestore?
- symfony - Symfony 5 - 自定义 Twig 错误模板
- sql - DB2 SQL 添加列值
- python - 为什么相同的代码会在 pycharm 和终端中产生不同的结果?
- r - 使用公式将 ifelse() 条件应用于数据框中的多个列以获取 NA 的值,该公式使用链接到列中另一个值的公式
- maven - 当质量门失败时,如何使 Maven 构建失败?
- .net-core - 在 Startup 中创建 ILogger 实例