Sunday, June 28, 2009

How Do Units Of Insulin Equate To Cc's?

How to Save a file in Python

First we must distinguish between text files and binary files. The first are those that if you open a text editor are readable (and understandable?) The latter do contain a series of strange characters.
Then what are the binaries? The advantage is that when you save the binary numbers is more efficient and therefore smaller. However, when you are a beginner to a binary file is a little 'difficult to manage, because we see through the examples.
Try saving the phrase "Hello World!" in ascii and binary
ASCII
# Open the file in write mode test
f = open ("test.txt", "w") # write the sentence
f.write (" Hello World! ")
# close the file f.close ()
File size: 12 Bytes
Binary
import struct # Open the file in write mode and binary provabin
f = open ("provabin.txt ',' w + b ')
# convert the sentence
struct.pack bin = ('12s ',' Hello World! ") #
the 12s indicates that we are converting a string of 12 characters
# write the sentence
f.write (bin )
# close the file f.close ()
File size: 12 Bytes
Try opening the file and compare them, maybe you will surprised ...
The two files are identical this is because the binary encoding of a character is the character himself, this is due to the amount of information managed by the binary data format.
aside the explanation of why it is so (easily retrievable on the Internet or on any computer book) we can see what is gained in using ascii or binary wanting to save the number 12345, it will in a text file Byte 9 (1 Character -> 1 Byte), whereas in a binary file it would take a total of 2 bytes (an integer -> 2 Bytes)
Now let's try to save 12,345 in ascii and binary
ASCII
# Open the file in write mode test
f = open ("test.txt", "w") # write the sentence
f.write ("12345") # close
file f.close ()
File Size: 5 Bytes
Binary
import struct # Open the file in write mode and binary provabin
f = open ("provabin.txt ", 'w + b') # convert the sentence
struct.pack bin = ('h', 12345)
# h indicates that we are running a short (2Byte)
# write the phrase
f.write (bin) # close the file
f.close ()
File size: 2 Byte