[Tutor] rf1

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 3 Dec 2001 22:25:02 -0800 (PST)


On Tue, 4 Dec 2001, Kirk Bailey wrote:

> import string
> 
> f = open("letter2.TXT",r')

Be careful --- you need to make sure to balance the quotes, or else Python
will think that the string is spilling over.  Strings that spill over will
be flagged as SyntaxErrors in Python, to make sure that you're aware of
this.

open(), by the way, will open things with "r"ead permission by default, so
the following code does the same thing:

    f = open("letter2.TXT")

Less typing, and less prone to mistakes.


> in1=f.readlines()
> f.close()
>
> print in1
>
> Looks like it formed a tupple if I grok this right.

Gotta be nitpicky about this.  *grin* To clarify, the command:

> in1=f.readlines()

is giving you back a list of the lines in your file.  There's a difference
between a tuple and a list.  Tuples use round parentheses:

   (1, 2, 3)

and lists use boxy braces:

   [1, 2, 3]

They're visually similar, and pretty much serve the same role as a holder
of possibly many values.  The big difference between tuples and lists,
though, is that lists are like balloons.  They're inflatable if we use
append() on them.


> But I wanted a dictionary! Those are SO much easier to search!

True: dictionaries are easy to search.  However, we need to tell Python
how exactly you want to search them.  Can you give a small example on what
you expect the dictionary to look like?