Efficient way to organise data file in columns with Python
Posted
by
user1700959
on Stack Overflow
See other posts from Stack Overflow
or by user1700959
Published on 2012-09-27T08:24:06Z
Indexed on
2012/09/27
9:37 UTC
Read the original article
Hit count: 184
I'm getting an output data file of a program which looks like this, with more than one line for each time step:
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
7.9819E-06 1.7724E-02 2.3383E-02 3.0048E-02 3.8603E-02 4.9581E-02 5.6635E-02 4.9991E-02 3.9052E-02 3.0399E-02
....
I want to arrange it in ten columns
I have made a Python script, using regular expressions to delete \n in the proper lines, but I think that there should be a simpler more elegant way to do it, here is my script:
import re
with open('inputfile', encoding='utf-8') as file1:
datai=file1.read()
dataf=re.sub(r'(?P<nomb>( \d\.\d\d\d\dE.\d\d){8})\n','\g<nomb>',datai)
with open('result.txt',mode='w',encoding='utf-8') as resultfile:
resultfile.write(datof)
Thanks in advance
© Stack Overflow or respective owner