a couple of newbie questions

Nick Vargish nav at adams.patriot.net
Fri Mar 21 19:26:33 CET 2003


"Joseph Paish" <jpaish at freenet.edmonton.ab.ca> writes:

> 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.

Put a '#' in front of each line...

#!/usr/bin/env python
import sys
print '\n# '.join(sys.stdin.read().split('\n'))

You'll get an extra line at the end, and no extra "#" on the first
line. Fixing it is left as an excercise (I love saying that).

> 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

datadict = {}
datafile = open(myfile, 'r')
for dataline in datafile.xreadlines():
    datadict[dataline.split(' ')[1]] = dataline

That should do the trick... 

> i have managed to read the datafile into a list of lists and would
  [ ... ]
> how to convert a list of lists into dictionaries.

Overkill... I think you're working it too hard.

> 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.

Indeed, see the above example (two lines, not counting initialization).

Hope that helps,

Nick

-- 
#include<stdio.h> /* SigMask 0.3 (sig.c) 19990429 PUBLIC DOMAIN "Compile Me" */
int main(c,v)char *v;{return !c?putchar(*v-1)&&main(0,v+ /* Tweaks welcomed. */
1):main(0,"Ojdl!Wbshjti!=obwAqbusjpu/ofu?\v\1");}  /* build: cc -o sig sig.c */




More information about the Python-list mailing list