Re: formatting file
SPJ wrote: > test11.1-1 installed > test11.1-1 update > test22.1-1 installed > test22.1-2 update > > I want the file to be formatted in the following way: > > test11.1-1 1.1-2 > test22.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) -- http://mail.python.org/mailman/listinfo/python-list
Re: formatting file
SPJ wrote: Hi, I am new to python hence posing this question. I have a file with the following format: test11.1-1 installed test11.1-1 update test22.1-1 installed test22.1-2 update I want the file to be formatted in the following way: test11.1-1 1.1-2 test22.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("""\ ... test11.1-1 installed ... test11.1-2 update ... test22.1-1 installed ... test22.1-2 update ... """) STeVe -- http://mail.python.org/mailman/listinfo/python-list
Re: formatting file
SPJ wrote: > I am new to python hence posing this question. > I have a file with the following format: > > test11.1-1 installed > test11.1-1 update > test22.1-1 installed > test22.1-2 update > > I want the file to be formatted in the following way: > > test11.1-1 1.1-2 > test22.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 "test11.1-1 update"; please be careful not to waste people's time who try to help...] -- George -- http://mail.python.org/mailman/listinfo/python-list
Re: formatting file
SPJ wrote: Hi, I am new to python hence posing this question. I have a file with the following format: test11.1-1 installed test11.1-1 update test22.1-1 installed test22.1-2 update I want the file to be formatted in the following way: test11.1-1 1.1-2 test22.1-1 2.1-2 How can I achieve this? I am stuck here. I suspect you need to a) remove the error in your specification (should there not be a "1.1-2 somewhere in the input) and/or b) explain the exact intent in a little more detail - we aren't psychic, and while the problem may be very obvious to you, we aren't inside your head with it! regards Steve -- Steve Holden+1 703 861 4237 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Python Web Programming http://pydish.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: formatting file
SPJ wrote: I have a file with the following format: test11.1-1 installed test11.1-1 update test22.1-1 installed test22.1-2 update I want the file to be formatted in the following way: test11.1-1 1.1-2 test22.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 -- http://mail.python.org/mailman/listinfo/python-list
formatting file
Hi, I am new to python hence posing this question. I have a file with the following format: test11.1-1 installed test11.1-1 update test22.1-1 installed test22.1-2 update I want the file to be formatted in the following way: test11.1-1 1.1-2 test22.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 -- http://mail.python.org/mailman/listinfo/python-list