Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Byte sequences in Python are represented using strings. The series of letters and symbols that you see when you print out a byte sequence is merely a printable representation of bytes that the string contains. To make use of this data, you usually manipulate it in some way to obtain a more useful representation.</p> <p>You can use <code>ord(x)</code> or <code>bin(x)</code> to obtain decimal and binary representations, respectively:</p> <pre><code>&gt;&gt;&gt; f = open('/tmp/IMG_5982.JPG', 'rb') &gt;&gt;&gt; data = f.read(10) &gt;&gt;&gt; data '\x00\x00II*\x00\x08\x00\x00\x00' &gt;&gt;&gt; data[2] 'I' &gt;&gt;&gt; ord(data[2]) 73 &gt;&gt;&gt; hex(ord(data[2])) '0x49' &gt;&gt;&gt; bin(ord(data[2])) '0b1001001' &gt;&gt;&gt; f.close() </code></pre> <p>The <code>'b'</code> flag that you pass to <code>open()</code> does not tell Python anything about how to represent the file contents. From <a href="http://docs.python.org/library/functions.html#open" rel="nofollow">the docs</a>:</p> <blockquote> <p>Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the 'b' has no effect.</p> </blockquote> <p><em>Unless you just want to look at what the binary data from the file looks like, Mark Pilgrim's book, Dive Into Python, has <a href="http://www.faqs.org/docs/diveintopython/fileinfo_divein.html#fileinfo.divein" rel="nofollow">an example</a> of working with binary file formats.</em> The example shows how you can read IDv1 tags from an MP3 file. The book's website seems to be down, so I'm linking to a mirror.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload