[Tutor] Reading from a file

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 18 Aug 2001 02:29:07 -0700 (PDT)


On Fri, 17 Aug 2001, Israel Evans wrote:

> this may not be the best way to do it but it seems to work... I'm rather new
> to this to.
> 
> >>> line = """"CVR",1,2"""
> >>> info = line.split(',')
> >>> info
> ['"CVR"', '1', '2']
> 
> >>> info.remove(info[len(info)-1])
> >>> info
> ['"CVR"', '1']
> 
> I'm just removing the last item in the list.  The /012 is probably
> some sort of line ending like \n?  I'm guessing.


Here's a sample interpreter session, annotated for your amusement:

###
>>> ord('\n')
10                      # Hmm... I expected to see 12.
>>> ord('\r')           # Maybe it's the carriage return character
13                      # Nope!

                        # ... oh, silly me.
                        # [thunk on the head]

>>> 012                 # I forgot \012 was in octal...
10                      # Ah, that's better.
###

So yes, '\012' is the newline '\n' character.  In older versions of
Python, unusual ASCII characters would be repr()esented in octal
notation.  Here's the paragraph from the "What's New in Python
2.1" document,

    http://www.amk.ca/python/2.1/

that talks about this:


"""Applying repr() to strings previously used octal escapes for
non-printable characters; for example, a newline was '\012'. This was a
vestigial trace of Python's C ancestry, but today octal is of very little
practical use. Ka-Ping Yee suggested using hex escapes instead of octal
ones, and using the \n, \t, \r escapes for the appropriate characters, and
implemented this new formatting."""