Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
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])

Sample output:

  trainee@kingston:~/m15py$ python imgsize
  Image is 90 x 28
  Image file size is 797 bytes
  trainee@kingston:~/m15py$


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].

(written 2015-03-09)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y117 - Python - Already written modules
  [2020] Learning Python - many new example programs - (2009-01-31)
  [2506] Good example of recursion in Python - analyse an RSS feed - (2009-11-18)
  [2890] Dates and times in Python - (2010-07-27)
  [2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
  [3442] A demonstration of how many Python facilities work together - (2011-09-16)
  [3465] How can I do an FTP transfer in Python? - (2011-10-05)
  [3479] Practical Extraction and Reporting - using Python and Extreme Programming - (2011-10-14)
  [4085] JSON from Python - first principles, easy example - (2013-05-13)
  [4086] Cacheing class for Python - using a local SQLite database as a key/value store - (2013-05-14)
  [4441] Reading command line parameters in Python - (2015-02-23)
  [4696] Programming with random numbers - yet re-using the same values for testing - (2016-06-22)
  [4697] Month, Day, Year number to day of week and month names in Python - English and Swedish - (2016-06-23)
  [4708] Scons - a build system in Python - building hello world - (2016-10-29)
  [4710] Searching a Json or XML structure for a specific key / value pair in Python - (2016-10-30)

P212 - Perl - More on Character Strings
  [453] Commenting Perl regular expressions - (2005-09-30)
  [583] Remember to process blank lines - (2006-01-31)
  [586] Perl Regular Expressions - finding the position and length of the match - (2006-02-02)
  [597] Storing a regular expression in a perl variable - (2006-02-09)
  [608] Don't expose your regular expressions - (2006-02-15)
  [737] Coloured text in a terminal from Perl - (2006-05-29)
  [928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
  [943] Matching within multiline strings, and ignoring case in regular expressions - (2006-11-25)
  [1222] Perl, the substitute operator s - (2007-06-08)
  [1230] Commenting a Perl Regular Expression - (2007-06-12)
  [1251] Substitute operator / modifiers in Perl - (2007-06-28)
  [1305] Regular expressions made easy - building from components - (2007-08-16)
  [1336] Ignore case in Regular Expression - (2007-09-08)
  [1510] Handling Binary data (.gif file example) in Perl - (2008-01-17)
  [1727] Equality and looks like tests - Perl - (2008-07-29)
  [1735] Finding words and work boundaries (MySQL, Perl, PHP) - (2008-08-03)
  [1947] Perl substitute - the e modifier - (2008-12-16)
  [2230] Running a piece of code is like drinking a pint of beer - (2009-06-11)
  [2379] Making variables persistant, pretending a database is a variable and other Perl tricks - (2009-08-27)
  [2657] Want to do a big batch edit? Nothing beats Perl! - (2010-03-01)
  [2801] Binary data handling with unpack in Perl - (2010-06-10)
  [2834] Teaching examples in Perl - third and final part - (2010-06-27)
  [2874] Unpacking a Perl string into a list - (2010-07-16)
  [2877] Further more advanced Perl examples - (2010-07-19)
  [2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
  [3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
  [3100] Looking ahead and behind in Regular Expressions - double matching - (2010-12-23)
  [3322] How much has Perl (and other languages) changed? - (2011-06-10)
  [3332] DNA to Amino Acid - a sample Perl script - (2011-06-24)
  [3411] Single and double quotes strings in Perl - what is the difference? - (2011-08-30)
  [3546] The difference between dot (a.k.a. full stop, period) and comma in Perl - (2011-12-09)
  [3630] Serialsing and unserialising data for storage and transfer in Perl - (2012-02-28)
  [3650] Possessive Regular Expression Matching - Perl, Objective C and some other languages - (2012-03-12)
  [3707] Converting codons via Amino Acids to Proteins in Perl - (2012-04-25)
  [3927] First match or all matches? Perl Regular Expressions - (2012-11-19)


Back to
Running an operating system command from your Python program - the new way with the subprocess module
Previous and next
or
Horse's mouth home
Forward to
Tcl variable names - no real limits!
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 http://www.wellho.net/horse/ - 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4452_Bin ... -Perl.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb