【问题标题】:How to insert into mongodb database a list of strings?如何在 mongodb 数据库中插入字符串列表?
【发布时间】:2013-03-05 00:05:30
【问题描述】:
database = mongo_connect()
un = str(session['username'])

database.game.insert({'host': session['username'],
    'player_list':[un]})

如果我这样做,那么当我检索 player_list 时,我会得到一个 unicode 列表。我怎样才能得到一个字符串列表?谢谢

【问题讨论】:

  • 为什么要strs 而不是unicodes?

标签: python flask pymongo


【解决方案1】:

因为mongodb stores data in bson format, and since bson is utf8-encoded, you'll get only unicode strings

无论如何,您都可以将unicode 编码为str

player_list = [x.encode('utf-8') for x in player_list]

【讨论】:

    猜你喜欢
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多