UnicodeEncodeError和surrogates not allowed

简介: UnicodeEncodeError和surrogates not allowed

当执行类似os.listdir() 这样的函数时,这些不合规范的文件名就会让Python陷入困境。 一方面,它不能仅仅只是丢弃这些不合格的名字。而另一方面,它又不能将这些文件名转换为正确的文本字符串。 Python对这个问题的解决方案是从文件名中获取未解码的字节值比如 \xhh 并将它映射成Unicode字符 \udchh 表示的所谓的”代理编码”。

如:字符 \udce4 是一个非法的Unicode字符。 它其实是一个被称为代理字符对的双字符组合的后半部分。 由于缺少了前半部分,因此它是个非法的Unicode。 所以,唯一能成功输出的方法就是当遇到不合法文件名时采取相应的补救措施。

surrogateescape:

这种是Python在绝大部分面向OS的API中所使用的错误处理器,

它能以一种优雅的方式处理由操作系统提供的数据的编码问题。

在解码出错时会将出错字节存储到一个很少被使用到的Unicode编码范围内。

在编码时将那些隐藏值又还原回原先解码失败的字节序列。

它不仅对于OS API非常有用,也能很容易的处理其他情况下的编码错误。


相关文章
|
3月前
|
关系型数据库 MySQL 数据安全/隐私保护
问题:ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
问题:ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
26 0
|
4月前
|
安全 关系型数据库 MySQL
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
26 2
|
安全 对象存储
set_time_limit() has been disabled for security reasons
set_time_limit() has been disabled for security reasons
157 0
set_time_limit() has been disabled for security reasons
|
关系型数据库 MySQL Java
Connection is read-only. Queries leading to data modification are not allowed
看了下mysql-connector-5.1.40版本中,如果设置failoverReadOnly=true (即默认值,参考链接),当mysql连接failover时,会根据jdbc连接串将当前连接的readOnly值设置为true (第8行代码) 1 2 3 4 ...
3399 0
Error saving your changes: Description control characters are not allowed
在修改 GitHub 上的仓库描述时出现此提示信息:Error saving your changes: Description control characters are not allowed 开始以为是 Fork 来的没有修改权限,但之前没有遇到这样的情况,提示信息说的也不是这个意思。
2369 0