Display a string of 1's and 0's in binary format Python

Is there any way to actually get a string of 1's and 0's from a binary format ?

# read in image data
fh = open('test.png','rb')
data = fh.read()
fh.close()

# write binary to text file
fh = open('test.txt','w')
fh.write(data)
fh.close
fh.close()

how can i turn that data string into 1's and 0's

Answers


This will convert each byte in your string to 8 binary digits and separate them with a space. You can easily change the separator.

data = " ".join(bin(ord(b))[2:].rjust(8, "0") for b in data)

Need Your Help

sqlite select minimum function value of grouped result

sql sqlite function group min

i have a table with gps locations of restaurants chains and would like to return the addresses of the restaurants closest to point (A) within a certain radius

jquery accordion: activate option using Struts <sj:accordion>

jquery jquery-plugins struts2 jquery-accordion

I have a Struts 2 application that is utilizing the accordion feature of the Struts2 jQuery plugin (http://code.google.com/p/struts2-jquery/wiki/AccordionTag) and I'm trying to programmatically tra...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.