newbie problem

Christopher Myers chris.myers at ingenta.com
Wed Aug 28 09:27:13 EDT 2002


I read the other posts after making my own solution to the problem, and
I still like mine better  :-).

Here it is (assuming > python 2.1):

#!/usr/local/bin/python
infile = open('inputfilenamehere').readlines()
for line in infile:
  if line[0] == "#":
    key = line.split()[2]
    continue
  else:
    values = line.split()
    values.insert(0, key)
    print '|'.join(values)


That's about as quick-and-dirty as I could come up with.
If you wanted to write to a file instead of print to the shell, just
replace the last line with:

    outfile.write('|'.join(values) + '\n')

of course after having opened outfile with:

    outfile=open('outputfilenamehere', 'w')

somewhere before the 'for' loop.

Or, redirect your output when you run the program.

Hope this helps (as getting a second opinion almost always helps me).

-Chris

Manuel Hendel wrote:
> 
> I'm totally new to python, I'm right know reading the book "Einführung
> in Python" from O'Reilly. I have to solve a problem and want to do it
> using python.
> I got a text file with the following input:
> 
> # KDNR 123456
> *                                     * domain   username
> # KDNR 234567
> info                           password domain   username
> info                           password domain   email-address
> info                           password domain   email-address
> *                              password domain   username
> 
> and need to get the following output:
> 
> 123456|*|*|domain|username
> 234567|info|password|domain|username
> 234567|info|password|domain|email-address
> 234567|info|password|domain|email-address
> 234567|*|password|domain|username
> 
> Can anyone give me a hint where to start and an idea how to solve
> this? The file has more than 17000 lines full of this.
> 
> Thank you very much in advance,
>     Manuel
> 
> --
> I have a higher and greater standard of principle. Washington could not lie. I
> can lie but I won't.
> -Mark Twain, in the Chicago Tribune, 20 Dec 1871

-- 
Christopher Myers, Graduate Software Developer 
Ingenta, Inc.
12 Bassett St.
Providence, RI  02903
ph:  401.331.2014 x 102
em:  chris.myers at ingenta.com
aim: chrismyers001



More information about the Python-list mailing list