开发者

encoding string

"afile" is a previously existing file.

handle=open("afile",'r+b')
data=handle.readline()
handle.close() 
#  signgenerator is a hashlib.md5() object

signgenerator.update(data)     
hex=signgenerator.hexdigest()
print(hex) # prints out 061e3f139c80d04f039b7753de5313ce

and write this to a file

 f=open("syncDB.txt",'a')
 #hex=hex.encode('utf-8')
 pickle.dump(hex,f)
 f.close() 

But when i read back the file as

while True:
    data=f.readline()
    print(data)

This gives the output:

b'\x80\x03X \x00\x00\x00061e3f139c80d04f039b7753de5313ceq\x00.\x80\x03X \x00\x00\x00d9afd4bb6bc57679f6b10c0b9610d2e0q\x00.\x80\x03X \x00\x00\x008b70452c46285d825d3670d433151841q\x00.\x80\x03X \x00\x00\x00061e3f139c80d04f039b7753de5313ceq\x00.\x80\x03X \x00\x00\x00d9afd4bb6bc57679f6b10c0b9610d2e0q\x00.\x80\x03X \x00\x00\x008b70452c46285d825d3670d433151841q\x00.\x80\x03X \x00\x00\x00b857c3b319036d72cb85fe8a679531b0q\x00.\x80\x03X \x00\x00\x007532fb972cdb019630a2e5a1373fe1c5q\x00.\x80\x03X \x00\x00\x000126bb23767677d0a246d6be1d2e4d5cq\x00.'

How do i encode to get the same hexdigest back from these bytes?? Also I am getting some gibberish characters in syncDb.txt like "€X" after each line.How do I correct开发者_如何学JAVAly write the data in a readable form??


You need to unpickle the data:

pickle.load(open('syncDB.txt', 'r+b'))

What you have there is pickled data. Proof:

>>> import pickle

>>> pickle.loads(b'\x80\x03X \x00\x00\x00061e3f139c80d04f039b7753de5313ceq\x00.\x80\x03X \x00\x00\x00d9afd4bb6bc57679f6b10c0b9610d2e0q\x00.\x80\x03X \x00\x00\x008b70452c46285d825d3670d433151841q\x00.\x80\x03X \x00\x00\x00061e3f139c80d04f039b7753de5313ceq\x00.\x80\x03X \x00\x00\x00d9afd4bb6bc57679f6b10c0b9610d2e0q\x00.\x80\x03X \x00\x00\x008b70452c46285d825d3670d433151841q\x00.\x80\x03X \x00\x00\x00b857c3b319036d72cb85fe8a679531b0q\x00.\x80\x03X \x00\x00\x007532fb972cdb019630a2e5a1373fe1c5q\x00.\x80\x03X \x00\x00\x000126bb23767677d0a246d6be1d2e4d5cq\x00.') '061e3f139c80d04f039b7753de5313ce'

But there's no point in pickling a hex string. You can just put it in the file. The pickle module should be used with more complex structures, like arrays, dicts, or even classes.


Don't pickle the hexdigest, just write it out as text.

with open("afile",'rb') as handle:
    data=handle.readline()

signgenerator.update(data)
hex=signgenerator.hexdigest()

with open("syncDB.txt",'ab') as f:
    f.write(hex + '\n')

with open("syncDB.txt",'rb') as f:
    for data in f:
        print(data)

If you really want to use pickle, you need to use the pickle.load function to read the data back from the file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜