2015-03-29 6 views
5

ho questo codice:TypeError: deve essere stringa, non unicode

... 
msgdict = {'datafile': datafile, 'mapper': mapper, 'reducer':reducer} 
msg = cPickle.dumps(msgdict) 
print msg 

Il msg stampa ottengo questo:

(dp1 
S'mapper' 
p2 
(S's3n://myFolder/mapper.py' 
p3 
tp4 
sS'datafile' 
p5 
(S's3n://myFolder/test.txt' 
p6 
tp7 
sS'reducer' 
p8 
(S's3n://myFolder/reducer.py' 
p9 
tp10 
s. 

Poi Im cercando di ottenere i miei contenuti:

for i in range(count): 
    m = q[0].read() 
    # this print returns a object Message 
    print m 
    # m.get_body()) returns the same of print msg above 
    msg = cPickle.loads(m.get_body()) 

Ma sto avendo questo errore:

msg = cPickle.loads(m.get_body())  
TypeError: must be string, not unicode 

Qualcuno sa come risolvere questo errore?

+0

Hai provato 'repr()' tutte le stringhe? –

+0

cos'è 'm',' q' o 'get_body'? – Daniel

risposta

7

provare a sostituire quella linea con il seguente:

msg = cPickle.loads(str(m.get_body())) 

Lanciando str()-m.get_body(), si fa in modo che se la stringa è unicode, si converte in una stringa.

+0

Grazie, la tua soluzione ha funzionato! – UserX

+0

@UserX nessun problema :) –