Page 160 -
P. 160

unsuitable format


           Default formats are unsuitable for files


          Although your data is now stored in a file, it’s not really in a useful format.
           Let’s experiment in the IDLE shell to see what impact this can have.








              Use a with statement to open your data file and display a single line from it:

              >>> with open('man_data.txt') as mdf:
                                                       Note: no need to close your file,
                     print(mdf.readline())
                                                       because “with” does that for you.
              ['Is this the right room for an argument?', "No you haven't!", 'When?', "No you didn't!", "You
              didn't!", 'You did not!', 'Ah! (taking out his wallet and paying) Just the five minutes.',
              'You most certainly did not!', "Oh no you didn't!", "Oh no you didn't!", "Oh look, this isn't
              an argument!", "No it isn't!", "It's just contradiction!", 'It IS!', 'You just contradicted
              me!', 'You DID!', 'You did just then!', '(exasperated) Oh, this is futile!!', 'Yes it is!']




          Yikes! It would appear your list is converted to a large string by print()
           when it is saved. Your experimental code reads a single line of data from the
           file and gets all of the data as one large chunk of text…so much for your code
           saving your list data.

           What are your options for dealing with this problem?





                                          Geek Bits





                                 By default, print() displays your data in a format that mimics
                                 how your list data is actually stored by the Python interpreter.
                                 The resulting output is not really meant to be processed further…
                                 its primary purpose is to show you, the Python programmer,
                                 what your list data “looks like” in memory.








           124    Chapter 4
   155   156   157   158   159   160   161   162   163   164   165