954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Converting a data file to particular format

Hi..

I have a file with title output.dat.format of this file is as follows

0	0.00E+00	0.00E+00	-2.00E-02	9.48E+02
1	0.00E+00	0.00E+00	-1.80E-02	9.48E+02


yes,Thats right.without any comma between.(just tab space)

i need to create a file from this output.dat which has form similar to

0,9.48E+02
0,9.48E+02


i.e the first and last column separated by comma and no white space in the entire line...I tried to code this by creating dictionary...i am unable to get the required output

PS:the initial file 1.e output.dat has 10,000 line in a format mentioned above

kaskoraja
Newbie Poster
10 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

You have zero in your input at second column, not first one. Input has float but you have output with integer string value, is output value integral
You can split task in two, just read in the float numbers with with open ...as. And process with strip and split methods, print out the desired columns as test.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

I am extremely sorry...the final output file should be the following

0,9.48E+02
1,9.48E+02
kaskoraja
Newbie Poster
10 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

So you can use this to give start (I leave out file i/o not to give all solution):

data = """0	0.00E+00	0.00E+00	-2.00E-02	9.48E+02
1	0.00E+00	0.00E+00	-1.80E-02	9.48E+02""".splitlines()
for d in data:
    line = d.strip().split()
    print line[0]+','+line[-1]
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

That solved my problem...thanks a lot!!

kaskoraja
Newbie Poster
10 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: