相关疑难解决方法(0)

将字符串打印为十六进制字节?

我有这个字符串:Hello world !!我想用Python打印它48:65:6c:6c:6f:20:77:6f:72:6c:64:20:21:21.

hex() 仅适用于整数.

怎么做到呢?

python string hex ordinal-indicator

152
推荐指数
9
解决办法
35万
查看次数

在 Python 中,如何替换字符串中的所有非 UTF-8 字符?

更新:真正的问题是 MySQL utf8 不支持四字节 UTF-8 字符。

关于这个主题有几个问题,但似乎没有一个完全是我的问题,除了这个,接受的答案对我不起作用。

我正在使用该MySQLdb模块在 Python 中进行编码,并且我想将一些文本放入 MySQL 数据库中。数据库配置为 UTF-8,但文本偶尔包含非 UTF-8 四字节 UTF-8 字符。

数据库修改的 Python 代码如下所示:

connection = MySQLdb.connect(
    'localhost',
    'root',
    '',
    'mydatabase',
    charset='utf8',
    use_unicode=True)
cursor = connection.cursor()
cursor.execute(
    'update mytable set entryContent=%s where entryName=%s',
    (entryContent, entryName))
connection.commit()
Run Code Online (Sandbox Code Playgroud)

它目前产生这个警告:

./myapp.py:233: Warning: Invalid utf8 character string: 'F09286'
  (entry, word))
./myapp.py:233: Warning: Incorrect string value: '\xF0\x92\x86\xB7\xF0\x92...' for column 'entry' at row 1
  (entryname, entrycontent))
Run Code Online (Sandbox Code Playgroud)

当我使用mysql命令行客户端查看实际进入数据库的内容时,我看到内容在第一次出现时被截断非 UTF-8 四字节 UTF-8 字符。

我不在乎保存 …

python mysql encoding utf-8

5
推荐指数
1
解决办法
9025
查看次数

标签 统计

python ×2

encoding ×1

hex ×1

mysql ×1

ordinal-indicator ×1

string ×1

utf-8 ×1