By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,510 Members | 1,687 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,510 IT Pros & Developers. It's quick & easy.

formatting file

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
SPJ wrote:
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
Jul 18 '05 #2

P: n/a
gry
SPJ wrote:
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

Jul 18 '05 #3

P: n/a
SPJ wrote:
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
Jul 18 '05 #4

P: n/a
SPJ wrote:
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)

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.