- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我需要在 Python 中获取 UTF-8 字符的八进制转义序列,并且想知道是否有任何更简单的方法来做我想做的事情,例如我忽略了标准库中的某些内容。我有一个临时的字符串操作函数,但我希望有更好的解决方案。
我想从(例如):𐅥
收件人:\360\220\205\245
现在我正在这样做:
char = '\U00010165' # this is how Python hands it over to me
char = str(char.encode())
# char = "b'\xf0\x90\x85\xa5'"
arr = char[4:-1].split(“\\x”)
# arr = ['f0', '90', '85', 'a5']
char = ''
for i in arr:
char += '\\' + str(oct(int(i,16)))
# char = \0o360\0o220\0o205\0o245
char = char.replace("0o", "")
有什么建议吗?
最佳答案
使用 format(i, '03o')
格式化为八进制数字,无需前导 0o
指示符,或 str.format()
以也包括文字反斜杠:
>>> format(16, '03o')
'020'
>>> '\\{:03o}'.format(16)
'\\020'
然后循环遍历编码的 bytes
值;每个字符都作为一个整数产生:
char = ''.join(['\\{:03o}'.format(c) for c in char.encode('utf8')])
演示:
>>> char = '\U00010165'
>>> ''.join(['\\{:03o}'.format(c) for c in char.encode('utf8')])
'\\360\\220\\205\\245'
>>> print(''.join(['\\{:03o}'.format(c) for c in char.encode('utf8')]))
\360\220\205\245
关于python - 在 Python 中八进制转义 UTF-8 字符的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21858021/
需要帮助将这些给定的数字打印成星号,但我是编程新手;我该怎么做? #include int main(void) { int a[5]={20,1,5,15,12}; int i=0
使用 Delphi XE 2 我试图确定缩放方向以将缩放效果应用于图像(TImage),但没有找到执行此操作的函数,并且图像的 OnGesture 事件中的 EventInfo 属性没有此信息. 我见
我不知道制服在内存中是如何表示的。 制服似乎会占用宝贵的寄存器空间,但它们最终会传入/通过/传出到全局内存中,对吗? 制服不用时情况会发生变化吗?编译器可以将它们优化掉吗?--在这种情况下,我已经将无
我正在尝试在名为“timeclock”的模型上记录“time_in”和“time_out”记录。这是我想做但无法开始工作的事情! 检查最后一个时钟条目,看看它是否同时填充了“time_in”和“tim
我想听听您如何解决这种编程任务!?每种类型(OPER = 1 类型)对应一种特定的信息。 这只是大约 10 个具有相同结构的规范之一。首选创建这些“转换器”(协议(protocol))的通用方法。 最
我正在使用 Rest API(NodeJS、Express)和 PostgreSQL 制作 React-Native 应用。 在我的本地机器上托管时一切正常。当 API 托管在我的机器上并且 Post
我是一名优秀的程序员,十分优秀!