views:

84

answers:

1

Hello,

I've got a little problem here. I'm converting binary to ascii, in order to compress data. All seems to work fine, but when I convert '11011011' to ascii and try to write it into file, I keep getting error

UnicodeEncodeError: 'charmap' codec can't encode character '\xdb' in position 0: character maps to

Here's my code:

    byte = ""
    handleR = open(self.getInput())
    handleW = open(self.getOutput(), 'w')
    file = handleR.readlines()
    for line in file:
        for a in range(0, len(line)):
            chunk = result[ord(line[a])]
            for b in chunk:
                if (len(byte) < 8):
                    byte+=str(chunk[b])
                else:
                    char = chr(eval('0b'+byte))
                    print(byte, char)
                    handleW.write(char)
                    byte = ""
    handleR.close()
    handleW.close()

Any help appreciated,

Thank You

+1  A: 

I think you want:

handleR = open(self.getInput(), 'rb')
handleW = open(self.getOutput(), 'wb')

That will ensure you're reading and writing byte streams. Also, you can parse binary strings without eval:

char = chr(int(byte, 2))

And of course, it would be faster to use bit manipulation. Instead of appending to a string, you can use << (left shift) and | (bitwise or).

EDIT: For the actual writing, you can use:

handleW.write(bytes([char]))

This creates and writes a bytes from a list consisting of a single number.

EDIT 2: Correction, it should be:

handleW.write(bytes([int(byte, 2)]))

There is no need to use chr.

Matthew Flaschen
Thank you for your answer, but how can I directly (with wb mode) write (str)"11011011" into file?No I get "TypeError: can't write str to binary stream"
Mikk
Thank you so much.Final working and version is handleW.write(bytes(chr(int(byte, 2)), "utf-8"))
Mikk
@Mikk, my previous answer was partly wrong. It can just be `handleW.write(bytes([int(byte, 2)]))`.
Matthew Flaschen