file handling return bike(*partsdef _record_from_bike(bike)return _bike_struct pack(bike identity encode("utf ")bike name encode("utf ")bike quantitybike pricesequence unpacking when we convert binary record into bikestock bike we first convert the tuple returned by unpack(into list this allows us to modify elementsin this case to convert utf- encoded bytes into strings with padding bytes stripped off we then use the sequence unpacking operator (*to feed the parts to the bikestock bike initializer packing the data is much simplerwe just have to make sure that we encode the strings as utf- bytes for modern desktop systems the need for application programs to use random access binary data decreases as ram sizes and disk speeds increase and when such functionality is neededit is often easiest to use dbm file or an sql database nonethelessthere are systems where the functionality shown here may be usefulfor exampleon embedded and other resource limited systems summary ||this showed the most widely used techniques for saving and loading collections of data to and from files we have seen how easy pickles are to useand how we can handle both compressed and uncompressed files without knowing in advance whether compression has been used we saw how writing and reading binary data requires careand saw that the code can be quite long if we need to handle variable length strings but we also learned that using binary files usually results in the smallest possible file sizes and the fastest writing and reading times we learned too that it is important to use magic number to identify our file type and to use version number to make it practical to change the format later on in this we saw that plain text is the easiest format for users to read and that if the data is structured well it can be straightforward for additional tools to be created to manipulate the data howeverparsing text data can be tricky we saw how to read text data both manually and using regular expressions xml is very popular data interchange format and it is generally useful to be able to at least import and export xml even when the normal format is binary or text one we saw how to write xml manually--including how to correctly escape attribute values and textual data--and how to write it using an element tree and dom we also learned how to parse xml using the element treedomand sax parsers that python' standard library provides