Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Editting txt file
#1
hello,
I would like to separate the in txt file but there is no space between columns. I did not get how to upload the file. So i am going to put a simple example of the file.If you look closely there should be 13 separate columns based on the titles. Please pay attention to width of each part. Only the width of 'nodal point' consists of 10 columns and the rest hsas the width of 12 columns in a line. How can I do that? thank you.

nodal point x-disp y-disp z-disp x-vel y-vel z-vel x-accl y-accl z-accl x-coor y-coor z-coor
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
Reply
#2
I see a space between the columns?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
lets look at the data (i.e. skip header row). there is space between some columns, but if value has sign, then there is no space. One approach would be to you can use regex to match the values in columns after the first one. In the example first column values has len=4 is this always the case?
Another approach would be to take first 4 chars as column 1 then every 12 chars as value (don't forget to strip leading space if there is one)
Your statement Only the width of 'nodal point' consists of 10 columns and the rest hsas the width of 12 columns in a line is clearly not true.
Reply
#4
Dear Buran;
The reason why I am saying there is no space between data is that you need to see the data in txt file. Once it has negative values the space is being closed. The length of values of first column is 4 that is correct;
but it takes 11 (not 10) lines in total. I do not know how I should attach the file so that you open it using text editor (notepad++ or etc.) I am a new user and i think i am not allowed to attach files.

Dear Wavic;
This is not the only data I have in the file, it just an illustration. once it gets negative , the space is being closed by negative mark.
Reply
#5
if I replicated the line correctly

 
def split_line(line):
    node, values = line[:10], line[10:]
    values = [values[start:start+12].strip() for start in range(0,144,12)]
    result_list = [node.strip()]
    result_list.extend(values)
    return result_list

my_line ='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'
print(split_line(my_line))
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']
now it is up to you to learn how to read the file line by line and use split_line finction to return list of strings
Reply
#6
or if you prefer using regex
 
import re
def split_line(line):
    pattern = r'[+-]?\d.\d{5}E[+-]\d{2}'
    regex = re.compile(pattern)
    values = regex.findall(line,11)
    result = [line[:11].strip()]
    result.extend(values)
    return result


my_line ='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'
print(split_line(my_line))
Reply
#7
there should be a way to read automatically like ;

data=file.readline()


Can I combine it your split_line(line) function and .append function? This is a long data. I cannot define them separately.

Thnak you
Reply
#8
what do you want to do with the data?
Reply
#9
Dear Buran;
Please see the attachment that has the full version of the data.

In each time step, there are output for each nodes like ( I will skip second set of the data for the same timestep which has rotational data ). Basically, I would like to create columns without defining line-by-line by hand. I need to have 13 columns in total for each time step. But because there is no space in data, it does not recognize columns and I cannot assign the columns.

Thank you.

Attached Files

.txt   nodout.txt (Size: 118.53 KB / Downloads: 149)
Reply
#10
OK, but there are extra lines, also there are two type sets of data - with 13 and 10 columns. also they repeat in different points in time. what the output would look like?
Reply


Forum Jump:

User Panel Messages

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