Domanda

Sto usando cdb per un database costante in Python. Vorrei associare gli ID interi con alcune stringhe e vorrei evitare di memorizzare ciascuno di questi ID interi come stringhe, e invece memorizzarli come numeri interi. cdb sta cercando una stringa o un buffer di sola lettura. Esiste un modo per memorizzare queste chiavi come numeri interi anziché come stringhe?

Ad esempio:

cdb = cdb.cdbmake("test.cdb","test.cdb.tmp")
key = 5
value = "some test string"

#this throws an error
maker.add(key,value)
#TypeError: add() argument 1 must be string or read-only buffer, not int

#this would work, but seems inefficient
maker.add(str(key),value)
È stato utile?

Soluzione

Secondo il sito web cdb il database accetta solo stringhe come chiavi

  

Un cdb è un array associativo: mappa stringhe (chiavi) a stringhe (dati).

Quindi dovrai prima convertire gli interi in stringhe. Ti suggerisco di avvolgere str in una funzione di utilità e dimenticare l'overhead.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top