Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Editting txt file
#11
how about splitting on +- or space.
Reply
#12
I will skip the second set ( which has information about rot). the range of columns for nodal point is from 1 to 11. And the other data takes 12 columns for each. and then i will use .append function to add the data set at next time step. Eventually I will have all x-disp, y-disp,,, etc of all time steps for each nodal points. Would you please help me just separate the data by columns so that the code will know between columns of 1-11 is nodal point, 11-23 is x-disp, 23-35 is y-disp,,, etc . Finally i will have 13 big columns so I can assign them to be x-disp, ydisp,,, etc. I will do the rest. Thank you .
Reply
#13
(Oct-13-2017, 08:40 PM)Larz60+ Wrote: how about splitting on +- or space.
there is also + or - in the number, e.g. 0.00000E+00
Reply
#14
import re
def parse_line(line):
    pattern = r'[+-]?\d.\d{5}E[+-]\d{2}'
    regex = re.compile(pattern)
    values = regex.findall(line,11)
    result = [line[:11]]
    result.extend(values)
    return '{} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12}\n'.format(*result)

input_file = 'nodout.txt' # path to your file
output_file = 'new_nodout.txt'

with open(input_file, 'r') as f, open(output_file, 'w') as f_out:
    for line in f:
        if len(line) == 155:
            f_out.write(parse_line(line))
        else:
            f_out.write(line)
this will produce the attached file
first column is with len 10, all other - len 13

Attached Files

.txt   new_nodout.txt (Size: 122.88 KB / Downloads: 157)
Reply
#15
Dear Buran;
I appreciate your help. This format will work very well, but there is a problem between lines of 100-127. There is a space between negative mark and the number. Would you please look into that? And How do I make sure It will not happen for the rest if I have huge size file? thank you
Reply
#16
there was a problem, now it is correct

 
import re
def parse_line(line):
    pattern = r'[+-]?\d.\d{5}E[+-]\d{2}'
    regex = re.compile(pattern)
    values = regex.findall(line)
    result = [line[:10]]
    result.extend(values)
    return '{} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12} {: >12}\n'.format(*result)


input_file = 'nodout.txt' # path to your file
output_file = 'new_nodout.txt'

with open(input_file, 'r') as f, open(output_file, 'w') as f_out:
    for line in f:
        if len(line) == 155:
            f_out.write(parse_line(line))
        else:
            f_out.write(line)

Attached Files

.txt   new_nodout.txt (Size: 122.48 KB / Downloads: 174)
Reply
#17
yeah, I noticed the problem myslef
Reply
#18
import re
def parse_line(line):
    pattern = r'[+-]?\d.\d{5}E[+-]\d{2}'
    regex = re.compile(pattern)
    values = regex.findall(line)
    result = [line[:10]]
    result.extend(values)
    template = ''.join(['{}', ' {: >12}'*12, '\n'])
    return template.format(*result)


input_file = 'nodout.txt' # path to your file
output_file = 'new_nodout.txt'

with open(input_file, 'r') as f, open(output_file, 'w') as f_out:
    for line in f:
        if len(line) == 155:
            f_out.write(parse_line(line))
        else:
            f_out.write(line)
just to remove the long ugly string
Reply
#19
After 20th try I think I did it. Don't know. It's a midnight here. Good night.
for line in text.split('\n')[1:]:
    l = []
    l.append(line[:4])
    l.extend([line[4:][i:i+12] for i in range(0, len(line[4:]), 12)])
    print(','.join([element.strip() for element in l]))
Output:
6000,1.28403E-03,-3.24947E-03,0.00000E+00,-2.26290E-02,-1.19560E-02,0.00000E+00,-1.06940E+00,-2.67490E+00,0.00000E+00,2.95513E+01,8.39675E+00,0.00000E+00 6001,1.19853E-03,-3.29378E-03,0.00000E+00,-2.24782E-02,-1.39476E-02,0.00000E+00,-8.40659E-01,-2.75136E+00,0.00000E+00,2.95512E+01,8.19671E+00,0.00000E+00 6002,1.19008E-03,-3.27726E-03,0.00000E+00,-2.10368E-02,-1.50427E-02,0.00000E+00,-9.82155E-01,-1.44342E+00,0.00000E+00,2.95512E+01,7.99672E+00,0.00000E+00 6003,1.12513E-03,-3.24679E-03,0.00000E+00,-1.73326E-02,-1.15935E-02,0.00000E+00,-2.25696E+00,-1.28769E+00,0.00000E+00,2.95511E+01,7.79675E+00,0.00000E+00 6004,1.07466E-03,-3.20675E-03,0.00000E+00,-1.69112E-02,-1.21906E-02,0.00000E+00,-2.15492E+00,3.66086E-01,0.00000E+00,2.95511E+01,7.59679E+00,0.00000E+00 6005,1.01320E-03,-3.13263E-03,0.00000E+00,-1.39944E-02,-9.26766E-03,0.00000E+00,-1.32388E+00,-3.82603E-02,0.00000E+00,2.95510E+01,7.39687E+00,0.00000E+00 6006,9.48647E-04,-3.05597E-03,0.00000E+00,-1.44646E-02,-8.38187E-03,0.00000E+00,-1.40907E+00,1.23504E+00,0.00000E+00,2.95509E+01,7.19694E+00,0.00000E+00 6007,8.85903E-04,-2.95333E-03,0.00000E+00,-1.27204E-02,-5.49422E-03,0.00000E+00,1.49626E-01,9.06790E-01,0.00000E+00,2.95509E+01,6.99705E+00,0.00000E+00 6008,8.12200E-04,-2.84560E-03,0.00000E+00,-1.24943E-02,-5.13839E-03,0.00000E+00,-6.06144E-01,2.37943E+00,0.00000E+00,2.95508E+01,6.79715E+00,0.00000E+00 6009,7.54015E-04,-2.71980E-03,0.00000E+00,-1.12431E-02,-2.51882E-03,0.00000E+00,-9.16694E-01,2.24073E+00,0.00000E+00,2.95508E+01,6.59728E+00,0.00000E+00 6010,6.81588E-04,-2.60371E-03,0.00000E+00,-1.14631E-02,-2.57489E-03,0.00000E+00,-9.40017E-01,2.49151E+00,0.00000E+00,2.95507E+01,6.39740E+00,0.00000E+00 6011,6.27836E-04,-2.46679E-03,0.00000E+00,-1.05504E-02,4.59373E-04,0.00000E+00,-2.92736E-01,2.02190E+00,0.00000E+00,2.95506E+01,6.19753E+00,0.00000E+00 6012,5.65892E-04,-2.33391E-03,0.00000E+00,-1.05454E-02,-8.70450E-05,0.00000E+00,-3.97478E-01,2.87375E+00,0.00000E+00,2.95506E+01,5.99767E+00,0.00000E+00 6013,5.20693E-04,-2.18806E-03,0.00000E+00,-9.38509E-03,2.47265E-03,0.00000E+00,-4.43511E-01,2.00480E+00,0.00000E+00,2.95505E+01,5.79781E+00,0.00000E+00 6014,4.69833E-04,-2.04523E-03,0.00000E+00,-8.89576E-03,2.91519E-03,0.00000E+00,-1.07431E-01,2.09896E+00,0.00000E+00,2.95505E+01,5.59795E+00,0.00000E+00 6015,4.29652E-04,-1.89813E-03,0.00000E+00,-7.46945E-03,5.55568E-03,0.00000E+00,-2.87285E-01,1.93713E+00,0.00000E+00,2.95504E+01,5.39810E+00,0.00000E+00 6016,3.80327E-04,-1.75164E-03,0.00000E+00,-6.77496E-03,5.49461E-03,0.00000E+00,-3.01855E-01,1.75250E+00,0.00000E+00,2.95504E+01,5.19825E+00,0.00000E+00 6017,3.40919E-04,-1.60056E-03,0.00000E+00,-5.67025E-03,8.00584E-03,0.00000E+00,-2.27148E-01,2.10710E+00,0.00000E+00,2.95503E+01,4.99840E+00,0.00000E+00 6018,2.96914E-04,-1.44683E-03,0.00000E+00,-4.62168E-03,7.30899E-03,0.00000E+00,-1.40161E-01,1.34875E+00,0.00000E+00,2.95503E+01,4.79855E+00,0.00000E+00 6019,2.62750E-04,-1.30132E-03,0.00000E+00,-3.69431E-03,8.93300E-03,0.00000E+00,-1.29424E-01,1.12608E+00,0.00000E+00,2.95503E+01,4.59870E+00,0.00000E+00 6020,2.28180E-04,-1.15778E-03,0.00000E+00,-3.18653E-03,7.88581E-03,0.00000E+00,-4.41126E-01,9.34947E-01,0.00000E+00,2.95502E+01,4.39884E+00,0.00000E+00 6021,1.98378E-04,-1.02328E-03,0.00000E+00,-2.19533E-03,8.93209E-03,0.00000E+00,1.18022E-01,1.47233E+00,0.00000E+00,2.95502E+01,4.19898E+00,0.00000E+00 6022,1.73392E-04,-8.94784E-04,0.00000E+00,-2.48349E-03,7.27540E-03,0.00000E+00,-7.74400E-01,4.29480E-01,0.00000E+00,2.95502E+01,3.99911E+00,0.00000E+00 6023,1.47810E-04,-7.76176E-04,0.00000E+00,-1.53367E-03,7.61353E-03,0.00000E+00,-8.22209E-02,7.12992E-01,0.00000E+00,2.95501E+01,3.79922E+00,0.00000E+00 6024,1.28678E-04,-6.62623E-04,0.00000E+00,-1.95532E-03,5.80315E-03,0.00000E+00,-8.20924E-02,5.31625E-01,0.00000E+00,2.95501E+01,3.59934E+00,0.00000E+00 6025,1.06069E-04,-5.59772E-04,0.00000E+00,-9.45175E-04,6.11427E-03,0.00000E+00,-2.60260E-01,6.06516E-01,0.00000E+00,2.95501E+01,3.39944E+00,0.00000E+00 6026,8.96906E-05,-4.60312E-04,0.00000E+00,-9.02148E-04,4.68633E-03,0.00000E+00,-1.42468E-01,3.43671E-02,0.00000E+00,2.95501E+01,3.19954E+00,0.00000E+00 6027,6.96937E-05,-3.71779E-04,0.00000E+00,-7.09436E-04,4.31467E-03,0.00000E+00,-1.74249E-01,3.25648E-01,0.00000E+00,2.95501E+01,2.99963E+00,0.00000E+00 6028,5.50085E-05,-2.87074E-04,0.00000E+00,-3.28184E-04,3.21905E-03,0.00000E+00,-3.90265E-01,-3.24212E-01,0.00000E+00,2.95501E+01,2.79971E+00,0.00000E+00 6029,3.82648E-05,-2.11207E-04,0.00000E+00,-3.61708E-04,2.61636E-03,0.00000E+00,1.72393E-01,4.45897E-01,0.00000E+00,2.95500E+01,2.59979E+00,0.00000E+00 6030,2.54675E-05,-1.38665E-04,0.00000E+00,-1.67079E-04,1.65165E-03,0.00000E+00,-4.80644E-01,9.47771E-02,0.00000E+00,2.95500E+01,2.39986E+00,0.00000E+00 6031,1.23007E-05,-7.18537E-05,0.00000E+00,-1.05629E-04,1.18883E-03,0.00000E+00,1.71887E-01,-2.09403E-01,0.00000E+00,2.95500E+01,2.19993E+00,0.00000E+00 6032,1.00965E-06,-8.14235E-06,0.00000E+00,-3.03979E-05,1.38452E-04,0.00000E+00,-1.05392E-01,-4.44715E-01,0.00000E+00,2.95500E+01,1.99999E+00,0.00000E+00 6033,7.82702E-07,-6.70334E-06,0.00000E+00,-7.34207E-05,8.24313E-05,0.00000E+00,-2.66645E-01,6.78411E-02,0.00000E+00,2.95500E+01,1.59999E+00,0.00000E+00 6034,5.84909E-07,-5.06756E-06,0.00000E+00,-1.27161E-05,7.73102E-05,0.00000E+00,-1.20816E-01,-3.83151E-01,0.00000E+00,2.95500E+01,1.19999E+00,0.00000E+00 6035,3.89695E-07,-3.48309E-06,0.00000E+00,-2.06166E-05,4.38646E-05,0.00000E+00,-5.24263E-02,2.27947E-03,0.00000E+00,2.95500E+01,7.99997E-01,0.00000E+00 6036,2.03733E-07,-1.75249E-06,0.00000E+00,-7.18774E-07,1.80417E-05,0.00000E+00,-3.79820E-02,-1.45955E-01,0.00000E+00,2.95500E+01,3.99998E-01,0.00000E+00 6037,2.21320E-08,0.00000E+00,0.00000E+00,5.57123E-07,0.00000E+00,0.00000E+00,7.11758E-06,0.00000E+00,0.00000E+00,2.95500E+01,0.00000E+00,0.00000E+00
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020