a couple of newbie questions

Robin Munn rmunn at pobox.com
Fri Mar 21 17:58:30 CET 2003


Joseph Paish <jpaish at freenet.edmonton.ab.ca> wrote:
> i am in the process of converting a perl script to python and have a
> couple of questions :
> 
> --- 1.  is there a way to comment out a large block of code in python?
> the reason i ask is that i would like to comment out the entire perl
> script and convert it piece by piece into python, uncommenting each
> function as it is converted so i can test if it does what i want.

You could enclose blocks of code in triple-quote characters:

""" like this...
    def foo(bar):
        return bar+1
"""

Triple-quotes quote everything, including newlines, until the next
triple-quote. Note that triple-quotes can be used with both the quote
character (") and the apostrophe ('). So if you already have
triple-quotes of the """ style in the function you're trying to comment
out, then you can enclose it in '''-style triple quotes.

See http://www.python.org/doc/current/ref/strings.html for all the
formal syntax details of triple-quoted strings.

The other alternative for commenting out large blocks of code is to make
an editor macro to insert # at the start of every line, but for what you
want to do, triple-quoting would be more efficient.

> 
> --- 2.  is there a way to read a datafile whose records are space
> separated so that the second field becomes the key to a dictionary and
> the value portion is the entire record.  for example,
> 
> 01/01/2003 abc 1234.56 789.98 12332.98 <<< the original record
> 
> the key would be : abc
> the value would be : 01/01/2003 abc 1234.56 > 789.98 12332.98
> 
> i have managed to read the datafile into a list of lists and would
> like to loop through this collection of records and convert it to a
> list of dictionaries keyed on the second field.  i have already looked
> everywhere i can think of, but i haven't had any luck finding how to
> convert a list of lists into dictionaries.
> 
> an alternative would be to bypass the list of lists altogether and
> populate the dictionary as i read each record.  this would be even
> simpler.
> 
> suggestions?

Read about the dict() built-in:

    http://www.python.org/doc/current/lib/built-in-funcs.html

Basically, you pass it a list of lists, where the inner list has two
element: first element is the key, second element is the value.

Or, even better, use something like the following approach:

    datadict = {}  # Make a new, empty dictionary
    inputfile = file('inputdata.txt', 'r')
    for line in inputfile:   # Works in Python 2.2 or later
        fields = line.split()
        datadict[fields[1]] = line
    # Voila, you're done!

That is by far the simplest way of doing what you're trying to do.

Notice especially the "fields = line.split()" line. If you're coming
from Perl, your instinct will probably be to call a function (like
split), passing it the string as a parameter. You can do things that way
in Python if you really want to; most of the functions you'll need are
found in the string module. But the Pythonic way of doing string
operations is to call methods of string objects. In Python, strings are
objects just like everything else, and can have methods (and attributes,
if you're feeling insane). So most functions in the string module are
deprecated, and you should be using methods of string objects to do your
string-processing work. See here for the string methods available:

    http://www.python.org/doc/current/lib/string-methods.html

I hope this helps.

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838




More information about the Python-list mailing list