Search code examples
pythonpython-2.7decodepython-unicode

converting byte to str python2.7


I have a dictionary

k ={'Creator': '\xfe\xff\x00M\x00i\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\xae\x00 \x00O\x00f\x00f\x00i\x00c\x00e\x00 \x00W\x00o\x00r\x00d\x00 \x002\x000\x000\x007'}

which is containing metadata of a pdf. In the pdf properties the Creator is Microsoft Office Word 2007. I am unable to convert k['Creator'] to 'Microsoft Office Word 2007' as in this case.


Solution

  • This gives the required string.

    Code :

    >>> print(k['Creator'].decode('utf16','ignore')).encode('utf-8') 
    Microsoft® Office Word 2007 
    >>> type((k['Creator'].decode('utf16','ignore')).encode('utf-8')) 
    <type 'str'>