formatting file

S

SPJ

Hi,

I am new to python hence posing this question.
I have a file with the following format:

test1 1.1-1 installed
test1 1.1-1 update
test2 2.1-1 installed
test2 2.1-2 update

I want the file to be formatted in the following way:

test1 1.1-1 1.1-2
test2 2.1-1 2.1-2

How can I achieve this? I am stuck here.

Thanks in advance.
spj



__________________________________
Yahoo! Messenger
Show us what our next emoticon should look like. Join the fun.
http://www.advision.webevents.yahoo.com/emoticontest
 
P

Peter Hansen

SPJ said:
I have a file with the following format:

test1 1.1-1 installed
test1 1.1-1 update
test2 2.1-1 installed
test2 2.1-2 update

I want the file to be formatted in the following way:

test1 1.1-1 1.1-2
test2 2.1-1 2.1-2

Please verify that you made an error about the second
version value for "test1": the before (1.1-1) and
the after (1.1-2) values do not match.

Also, is it fair to assume the file is guaranteed
to contain even numbers of lines, always with both
an "installed" line and an "update" line, and that
the order is as shown above, etc... in other words,
how robust does the solution need to be?

(You might also show a little bit of your own attempt,
if nothing else so we don't get the idea this is
homework that we're doing for you. :) )

-Peter
 
G

gry

SPJ said:
I am new to python hence posing this question.
I have a file with the following format:

test1 1.1-1 installed
test1 1.1-1 update
test2 2.1-1 installed
test2 2.1-2 update

I want the file to be formatted in the following way:

test1 1.1-1 1.1-2
test2 2.1-1 2.1-2

For data that has a clear tree structure with keys, a quick solution
is often a dictionary, or dictionary of dictionaries. The setdefault
idiom below is very handy for this sort of thing. The test name
"test1"
is key to the top dict. The operation "update" is the key to the sub
dictionary. Setdefault returns the dict for the specified test, or a
new dict if there is none.

..d={}
..for line in open('tests.txt'):
.. test,version,operation = l.split()
.. d.setdefault(test,{})[operation] = version

..for test,d in d.items():
.. print test, d['installed'], d['update']

[BWT, your given test data appears to have been
wrong "test1 1.1-1 update"; please be careful not to waste
people's time who try to help...]

-- George
 
S

Steven Bethard

SPJ said:
Hi,

I am new to python hence posing this question.
I have a file with the following format:

test1 1.1-1 installed
test1 1.1-1 update
test2 2.1-1 installed
test2 2.1-2 update

I want the file to be formatted in the following way:

test1 1.1-1 1.1-2
test2 2.1-1 2.1-2

How can I achieve this? I am stuck here.

py> import itertools as it
py> for line1, line2 in it.izip(f, f):
.... line1, line2 = [line.split() for line in [line1, line2]]
.... print '\t'.join(line1[:2] + line2[1:2])
....
test1 1.1-1 1.1-2
test2 2.1-1 2.1-2

where f is the file containing your data. I assumed it looked like:

py> import StringIO as strio
py> f = strio.StringIO("""\
.... test1 1.1-1 installed
.... test1 1.1-2 update
.... test2 2.1-1 installed
.... test2 2.1-2 update
.... """)

STeVe
 
P

Peter Otten

SPJ said:
test1    1.1-1   installed
test1    1.1-1   update
test2    2.1-1   installed
test2    2.1-2   update

I want the file to be formatted in the following way:

test1    1.1-1   1.1-2
test2    2.1-1   2.1-2

The following program expects a sorted input file:

import itertools
from operator import itemgetter

def pivot(infile, outfile,
getkey=itemgetter(0), getvalue=itemgetter(1), sep="\t"):
if not hasattr(infile, "read"):
infile = file(infile)
if not hasattr(outfile, "write"):
outfile = file(outfile, "w")

records = (line.split() for line in infile)
for key, items in itertools.groupby(records, getkey):
out_record = [key]
out_record.extend(getvalue(item) for item in items)
print >> outfile, sep.join(out_record)

if __name__ == "__main__":
import sys
infile, outfile = sys.argv[1:]
if infile == "-": infile = sys.stdin
if outfile == "-": outfile = sys.stdout
pivot(infile, outfile)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,234
Messages
2,571,178
Members
47,811
Latest member
Adisty

Latest Threads

Top