54 reading and writing binary data python cookbook pdf

No prior knowledge of Python is assumed you will learn how to install it in this tutorialbut it would 54 reading and writing binary data python cookbook pdf advantageous. 54 reading and writing binary data python cookbook pdf you complete this tutorial, you will have the skills to create powerful Python applications that can communicate and interact with 54 reading and writing binary data python cookbook pdf IBM DB2 database and harness the power that pureXML offers.

The IBM DB2 database management system has long been a leading player in the area of relational data management. In recent years, however, there has been a significant rise in the requirement for data structures that are more flexible and document-oriented in nature.

One of the more prominent examples of such data structure is XML. While many relational database systems have rushed to incorporate some form of XML support in their database, IBM DB2 is the only such offering that allows XML to be stored natively in the database, unchanged and true to its original form.

In this tutorial, you will develop a Python script that connects to the United States Census Bureau Web site and downloads a CSV file containing data about the population at a national, regional, and state-wide level—including the results of the Census and fluctuations based on estimates in each year since then. You will then learn how to process this data, converting it into an XML document. Rather than import this large document and rely on DB2 functions to slice and dice it into individual rows, you will then use Python to insert this data into DB2, with 54 reading and writing binary data python cookbook pdf XML document stored per each relevant row in the CSV file.

Finally, you will create a command-line application that produces some useful reports on this data, showing a list of states, regions, or countries in the order of highest to lowest population. To follow the steps in this tutorial, you will need to have the following software installed:. See Related topics for the links to download these prerequisites. This tutorial assumes that you are using a Microsoft Windows operating system, preferably XP or later. You will create three tables: Each of these 54 reading and writing binary data python cookbook pdf will store a unique ID for each row in the table, as well as an XML document that will house the census data that you will import from the U.

Census Bureau's CSV files later 54 reading and writing binary data python cookbook pdf this tutorial. 54 reading and writing binary data python cookbook pdf process can take a minute or two to complete so be patient. When it is finished, you will receive a response message like: Now, connect to the newly-created census database using the following: Once again, you should receive a response from the DB2 server, this time something along the lines of: A JDBC connection to the target has succeeded.

The database is now created and you are ready to create the tables that will store the application's data. You will load the population data into the database and store it in three separate tables: Let's create these tables now in Listing 1. Each of these tables store the same type of data—a unique identifier that will be automatically generated by DB2 each time a row is inserted, and an XML data column that will store an XML document for each row. Strictly speaking, you can use a single table here and create a type column on it to determine whether a row is a country, region, or state, but if you separate them into tables it you have more flexibility for future manipulation.

The SQL command completed successfully. Python is a high-level programming language that places a strong focus on the readability of code. Unlike many other programming languages, where code indentation and style is at the discretion 54 reading and writing binary data python cookbook pdf the developer, in Python you must use indentation to denote blocks of code such as classes, if statements, and loops.

Python is easy to learn, produces elegant and clean code, and is widely supported on a host of different platforms, making it an excellent choice for any number of different application development projects. Fortunately, you can download Python from the Web and install it on Windows—and you will learn how to do so in this section. Before you start, however, it's worth mentioning that you have a number of options when it comes to downloading Python for Windows.

The first option is to use the open-source official binary installer, available for download from the official Python Web site. This option offers the most up-to-date version of Python and is provided on an open-source license. In this tutorial, you will work with this distribution of Python.

Alternatively, the commercial ActiveState Python offers some additional resources such as complete documentation, and additional Python extensions including Windows-specific extensions that facilitate the development of Win32 API-based applications using Python. The first step in installing Python is to download it from the official Python Web site see Related topics for a link.

At the time of writing, the current production versions of Python are 2. This tutorial assumes you are using the 2. Save this file to your hard drive and when it has finished downloading, open the. Leave the default selection, Install for all usersand press Next to continue. You will now be asked to select a destination directory. The default should be C: You will now be offered the opportunity to customize your Python installation by selecting which features you want to be installed. By default, everything is selected, so leave this as is and press Next to start the installation.

The process will take a couple of minutes to complete, and when it is finished, you will see a window like the one in Figure 1. View image at full size. Press Finish to exit the setup application. Before you move on, it's worth verifying that Python is installed and working correctly.

You can use the shortcuts that were added to the Windows Start Menu if you wish, but I recommend that you launch Python from the command prompt as this is how you will run the scripts you create later in this tutorial. At the prompt, enter the command: See a text-only view of Figure 2. If you see a message such as python is not recognized as an internal or external command, operable program or batch filethe Python directory was not placed on your Windows Path.

See Related topics for information on how to set this up. To quit the Python prompt, enter the following command: You should return to the Windows command prompt after entering this command at the Python prompt. Navigate to the setuptools package page see Related topics and find the file for your version of Python, which is 2.

When it is finished, you will be returned to the Windows command prompt, as in Figure 3. See a text-only view of Figure 3. Open a Windows command prompt window and issue the python command to launch the Python interpreter.

At the prompt, enter the following commands to connect to DB2 and count the number of rows in the country table. Also, be sure to replace the credentials in the code in Listing 2 with your actual DB2 credentials. After you enter the final line above, press enter and the code will execute.

You should see a result Count: Also verify that your credentials for connecting to DB2 are correct. With the database set up and Python ready to get to work, you are now ready to start developing the main subject of this tutorial. In the next section, you will download, parse, and convert CSV data from the U.

You will then learn how to read this data from the database and display it to the user. Before you start, you should create a folder somewhere on your 54 reading and writing binary data python cookbook pdf disk where you will store the project files. I stored my data in a folder C: The United States Census Bureau has a plethora of data available for download, in a variety of different formats. Unfortunately, the population data from Census and estimates for each year since then is only available in CSV format and not XML.

Instead, however, you will create a Python script to do this task. In your favorite text editor, create a new file and save it as download. Add the code from Listing 3 to this file. In this script, you use the httplib module to connect to the census. Then you fetch the response and write it to a file named data. To run this script, open up the Windows Command Prompt and change to the project directory as follows: Next, run the following command to run the Python script: When the script has completed you will return to the prompt.

You might wonder why there were no messages produced—don't worry, this is a 54 reading and writing binary data python cookbook pdf thing as it means no errors occurred. Open your project folder in Windows Explorer and you will now notice an extra file in the folder named data.

If Excel asks you to save the file, choose No. If you accidentally 54 reading and writing binary data python cookbook pdf the file, simply delete it and re-run the download.

To convert the CSV data into XML, you must first be clear on how exactly you wish to store the data, whether different records should be stored differently, and check if some records should be discarded. In the example of the CSV file you just downloaded, you will notice that this contains three types of data: The 54 reading and writing binary data python cookbook pdf row of the file is a header row that is to be used for column names.

The script you create in this section will take the header row and use this data to form the tag names for each element that a record should have in the XML document. The script will determine, based on the first four columns, whether the particular row refers to a country, region, or state, and will set the tag name accordingly to indicate what the XML document refers to. Finally, the script will choose to exclude the Puerto Rico Commonwealth record as it has some incomplete data.

In your text editor, create a new file and save it as convert. Add the code from Listing 4 to this file. In this file, you use the csv library to read the data. Then you loop through each line of the CSV file.

If the current line is the first line of the file, you set that record as the header. This will be used later in the script as the element name for each field in a country, region, or state record. If the current line is not the header record, you loop through each column in the record, and create an inner XML element string whose name is driven from the heading record.

Finally, you check if the record contains an X in a specific field, and if so, set a Boolean indicator to True that will stop that particular row from being added to the XML document. The first way you can run this script is the same as before, by issuing:

Computers use binary, or base 2, to store data. The binary system has only two possible digits, the 0 and the 1. To understand binary, we will start with something everyone is familiar with: Base 10 has ten digits, from 0 through 9.

Numbers greater than 9 are represented by altering the position of digits within the number. Now for a more complex number: The carat symbol represents an exponent.

There's a pattern—with the increasing digits, the number is multiplied by an increasing power of the base 54 reading and writing binary data python cookbook pdf this case, How do you represent a number with two digits?

The number one, of course, 54 reading and writing binary data python cookbook pdf one, but the number 10 is two, is eight, and so on. Since computer scientists often mix number systems, the prefix 0b is placed in front of a binary number. So what are bits and bytes? The word bit is a shortening of the term bi nary digi t.

One bit of memory holds a single binary digit. The byte is a grouping of eight bits. The eight-bit byte can conveniently store a single character. There are possible combinations of 8 bits.

A kilobyte KB is equal to bytes. A megabyte MB is equal to kilobytes, and bytes. A gigabyte GB is equal to 1 about billion bytes. A Terabyte is equal to 1 trillion bytes. A petabyte is equal to 1 quadrillion bytes. The greatest order of magnitude in common use is the exabytewhich approximately equals 1 quintillion bytes. Nearly all computers use binary. The system fits neatly into the two states of a single bit memory: The computer can only read directions given in binary form, which makes binary the computers natural language.

The Binary Code is the syntax that a computer uses to handle data. For example, a computer understands "HI" as These codes are used to represent all characters that can appear in data such as numbers, letters, and special characters and symbols like the dollar sign, comma, percent symbol and many mathematical characters.

It consists 1 to 4 bytes per character and is capable of representing over a million characters. Unicode is used by the majority of web related browser and applications with in browsers. The greatest advantage of using Unicode is that it can be used across the world and maintain consistent results. Understanding Computers Today and Tomorrow. Processing and Memory, 2, page In this way, software programs must also be represented by 0s and 1s.

Machine language is also one of the binary code to convert the instruction before any program instruction is executed by computer. The computer uses a coding system to represent data computer language. Most computers today are considered binary computers digital computers which only recognize two states, on and off which are represented 54 reading and writing binary data python cookbook pdf the numbers 0 and 1.

Information entered into the computer is translated into computer form, and then processed back to the user in a form that can be understood by us. Like bits, there are many different units of data which are bits combined to form these units such as; byte, kilobyte, megabyte, gigabyte, and terabyte there are many more.

Similar to numeric and text based data, graphics, audio, and videos are also represented in a binary system. From Wikibooks, open books for an open world. Decimal Binary 0 0 1 1 2 10 3 11 4 5 6 7 8 9 10 Binary [ edit ] Computers use binary, or base 2, to store data. Computers and Binary [ edit ] Nearly all computers use binary.

Retrieved from " https: Views Read Edit View history. Policies and guidelines 54 reading and writing binary data python cookbook pdf us. In other languages Add links. This page was last edited on 13 Aprilat By using this site, you agree to the Terms of Use and Privacy Policy.