【问题标题】:python sqlite3 hypen character read problempython sqlite3 hypen字符读取问题
【发布时间】:2021-06-21 14:29:32
【问题描述】:

我有将这些值插入到 python 中的 sqlite 数据库的脚本:

items = [
['Lawnmower','Tool', 0, 150,'Excellent', '2012‐01‐05'],
['Lawnmower','Tool', 0, 370,'Fair', '2012‐04‐01'],
['Bike', 'Vehicle', 0, 200,'Good', '2013‐03‐22'],
['Drill', 'Tool', 0, 100,'Good', '2013‐10‐28'],
['Scarifier','Tool', 0, 200,'Average', '2013‐09‐14'],
['Sprinkler','Tool', 0, 80,'Good', '2014‐01‐06']
]

这是将值插入数据库的方法:

    cur.executemany(item_sql, items)
    db.commit()

我有一个这样的函数,它可以读取这些值,但它没有正确显示连字符符号,因为它可以在图像中看到。手动输入的最后一行显示正确:

    def get_items():
        query = '''
        select * from item'''
        return cursor.execute(query).fetchall()

python interpreter

但在 sqlite 软件中,hypen 符号显示为它应该是:

nothing wrong when viewed through sqlite software

有人知道为什么以及如何解决吗?

【问题讨论】:

  • items 是您要插入数据库的 准确 值吗?看起来“连字符”字符在解释器使用的字体中无法表示,这表明它可能是其他一些看起来像连字符的字符。

标签: python sqlite


【解决方案1】:

@snakecharmerb 走在正确的轨道上。那些未呈现的连字符是U+2010 Hyphen,而不是看起来相似的ASCII范围U+002D Hyphen-Minus,如果你想在Sqlite中对这些日期做任何事情,应该使用它。

您可以通过在编辑器中进行搜索和替换来修复它(实际上,这可能是因为用这些字符替换了正常的连字符,但如果这样做的话,我希望引号也会被更改),或类似perl -C24 -pi -e 's/\x{2010}/-/g' yourfile.py 的东西。可能有一些 python 等价物,但我不太了解。

或者你可以用类似的东西来修复现有的数据库

UPDATE item SET DateRegistered = replace(DateRegistered, char(0x2010), '-');

【讨论】:

  • 不错!我没有意识到 ASCII 连字符是 HYPHEN-MINUS 而不是 HYPHEN。每天都是上学日...
猜你喜欢
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
  • 1970-01-01
  • 2012-03-06
  • 1970-01-01
  • 2012-04-06
相关资源
最近更新 更多