473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

formatting file

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
4 1046
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: scooterm | last post by:
### Question Can anyone name a text editor or XML editor (prefer open source) that can open a simple table XML file in a spreadsheet-type GUI, without adding any extra formatting or junk to the...
5
by: Tom Petersen | last post by:
I am using a response.write to test the formatting of the output. I am supposed to get this: BEGIN:VCALENDAR VERSION:1.0 BEGIN:VEVENT DTSTART:20051022T090000Z DTEND:20051022T090000Z...
6
by: Tom Petersen | last post by:
Here is a little more info, sorry should have explained what my final goal was. I am creating a .vcs file from a form to import into Outlook. I was just testing the output on screen then pasting...
6
by: Ot | last post by:
I have an xml document similar to this <all> <one> <options attr1="1" attr2="2" /> <item name="a name" attr1="1" /> <item name="another" attr1="2"/> </one> <two> <options attr1="1"...
1
by: Riko Eksteen | last post by:
Hi I'm reading an xml file into an XmlDocument, adding some nodes, and writing it back out. I would like the nodes I add to assume the same level of indeting as the rest of the document. (I load...
2
by: Colleyville Alan | last post by:
I am using Access and have embedded the ActiveX control Formula One that came with Office 2000. (ver 3.04). I have created and formatted a spreadsheet and now I want to copy the info with...
0
by: marfi95 | last post by:
I have an xml file that I want to maintain the whitespace on and spacing between my sections. I have something like this. <!-- Comment lines A --> <!-- Comment lines A --> <A> <B>test</B>...
25
by: mdh | last post by:
Hi Group, Not looking for an answer, but more of an explanation. Thinking back to those heady days when you had the time to do them, may I ask this. Exercise 1-22 asks for a program to "fold"...
8
by: gopal | last post by:
Hi I am trying to write to log file but the formatting of string is not properly aligned in log file result The results looks some thing like this OK move.xml Successful OK ...
10
by: afromanam | last post by:
Regards, Please help What I'm trying to do is this: (and I can't use reports since I must export to Excel) I export some queries to different tabs in an excel workbook I then loop through...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.