Domanda

I use the bitstring module. What is the easiest way to convert a bitstring to an utf-8 string?

È stato utile?

Soluzione

You can use the .bytes property on a BitVector to convert the sequence of bits in the vector into the same sequence of bits in the form of a Python (2.7) str object.

Because strs are encoding-agnostic, you could consider yourself done at this point, provided that the sequence of bits that you stored in your vector correspond to a UTF-8--encoded Unicode string. (In other words, using this approach it's up to you to store bits in your vector that correspond to some UTF-8--encoded sequence of Unicode characters.)

Altri suggerimenti

As mentioned in bitstring module documentation:

# To covert to an ordinary string use the bytes property
open('video.m2v', 'wb').write(s.bytes)

Information available here.

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