For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Binary data handling - Python and Perl
Handling binary data has become a somewhat rarer requirement over tiem, but that doensn't mean the need has gone away - and on last week's Python course, my delegates had a requirement to read data in this format.
First and foremost with binary data, you need to understand what you're looking to read and write, and it's likely to vary from one person to the next. As an example when I'm training, I demonstrate the functionallity of reading binary data by reading the headers of a .gif file - where the first 10 bytes are:
• the letters GIF
• the subtype (87a or 89a) as 2 characters
• 16 bit integer, big endian, image width
• 16 bit integer, big endian, image height
Here's my sample code - open the file and read
- which will default to reading the whole file:
fh = open("amazon.gif","r")
bytes = fh.read()
Then from within the struct
module interpret the bytes, as described above:
type, subtype, width, height= struct.unpack("3s3shh",bytes[:10])
trainee@kingston:~/m15py$ python imgsize
Image is 90 x 28
Image file size is 797 bytes
Complete sources [here]
In writing this article, I looked up some of the previous examples I've written to handle biary data, and came across the same interprettation in Perl - see [here]
, with illustrations from a course I gave a couple of years back in Nurmberg, and with further examples of Perl's pack and unpack [here].
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesY117 - Python - Already written modules
 Learning Python - many new example programs - (2009-01-31)
 Good example of recursion in Python - analyse an RSS feed - (2009-11-18)
 Dates and times in Python - (2010-07-27)
 Syncronise - software, trains, and buses. Please! - (2010-08-22)
 A demonstration of how many Python facilities work together - (2011-09-16)
 How can I do an FTP transfer in Python? - (2011-10-05)
 Practical Extraction and Reporting - using Python and Extreme Programming - (2011-10-14)
 JSON from Python - first principles, easy example - (2013-05-13)
 Cacheing class for Python - using a local SQLite database as a key/value store - (2013-05-14)
 Reading command line parameters in Python - (2015-02-23)
 Programming with random numbers - yet re-using the same values for testing - (2016-06-22)
 Month, Day, Year number to day of week and month names in Python - English and Swedish - (2016-06-23)
 Scons - a build system in Python - building hello world - (2016-10-29)
 Searching a Json or XML structure for a specific key / value pair in Python - (2016-10-30)
P212 - Perl - More on Character Strings
 Commenting Perl regular expressions - (2005-09-30)
 Remember to process blank lines - (2006-01-31)
 Perl Regular Expressions - finding the position and length of the match - (2006-02-02)
 Storing a regular expression in a perl variable - (2006-02-09)
 Don't expose your regular expressions - (2006-02-15)
 Coloured text in a terminal from Perl - (2006-05-29)
 C++ and Perl - why did they do it THAT way? - (2006-11-16)
 Matching within multiline strings, and ignoring case in regular expressions - (2006-11-25)
 Perl, the substitute operator s - (2007-06-08)
 Commenting a Perl Regular Expression - (2007-06-12)
 Substitute operator / modifiers in Perl - (2007-06-28)
 Regular expressions made easy - building from components - (2007-08-16)
 Ignore case in Regular Expression - (2007-09-08)
 Handling Binary data (.gif file example) in Perl - (2008-01-17)
 Equality and looks like tests - Perl - (2008-07-29)
 Finding words and work boundaries (MySQL, Perl, PHP) - (2008-08-03)
 Perl substitute - the e modifier - (2008-12-16)
 Running a piece of code is like drinking a pint of beer - (2009-06-11)
 Making variables persistant, pretending a database is a variable and other Perl tricks - (2009-08-27)
 Want to do a big batch edit? Nothing beats Perl! - (2010-03-01)
 Binary data handling with unpack in Perl - (2010-06-10)
 Teaching examples in Perl - third and final part - (2010-06-27)
 Unpacking a Perl string into a list - (2010-07-16)
 Further more advanced Perl examples - (2010-07-19)
 Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
 Object Orientation in an hour and other Perl Lectures - (2010-11-18)
 Looking ahead and behind in Regular Expressions - double matching - (2010-12-23)
 How much has Perl (and other languages) changed? - (2011-06-10)
 DNA to Amino Acid - a sample Perl script - (2011-06-24)
 Single and double quotes strings in Perl - what is the difference? - (2011-08-30)
 The difference between dot (a.k.a. full stop, period) and comma in Perl - (2011-12-09)
 Serialsing and unserialising data for storage and transfer in Perl - (2012-02-28)
 Possessive Regular Expression Matching - Perl, Objective C and some other languages - (2012-03-12)
 Converting codons via Amino Acids to Proteins in Perl - (2012-04-25)
 First match or all matches? Perl Regular Expressions - (2012-11-19)
Some other Articles
Objects in Tcl - iTcl - updated first steps example
Working out distance between places, using OS grid references and a program in Tcl
Everything is a string - even a list
Tcl variable names - no real limits!
Binary data handling - Python and Perl
Running an operating system command from your Python program - the new way with the subprocess module
Deciding whether to use parameters, conditional statements or subclasses
Spike solution, refactoring into encapsulated object methods - good design practise
What is the difference between a function and a method?
Overnight accommodation in Melksham - if Well House Manor is full!
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).