Skip to content Skip to sidebar Skip to footer

Need Help For Extracting Columnar Data

I wrote the code as below its extracting all tabular data by splitting. Now i want to extract the last column(Values) data by removing Text in the middle of the table. import r

Solution 1:

If I've understood your question, there's several things wrong with your code. Notably, you can't read and write to same file in the manner shown. To complicate the situation, there are nestedforloops, both reading lines from the same input file.

Here's a revised version that avoids those problems by writing to a separate output file and doing everything under the control of single for loop. I also duplicated the line of data in the input file and changed all the numbers on it so they start with a 9 so there would be more than one line of data for testing purposes.

import re

withopen('filename','r') as f_read, open('filename2','w') as f_write:
    for line in (line.strip() for line in f_read):
        if re.search(r'^\d+', line):  # line starts with a number?
            columnar_data = ' '.join(line.split())
            print columnar_data
            f_write.write(columnar_data+'\n')

Contents of output file (filename2):

121 3423 342 4545 45435 4345 42353 3456456 67658 3435
921 9423 942 9545 95435 9345 92353 9456456 97658 9435

Solution 2:

I can't quite tell what you are trying to do. But if all you want to do is read in the data from the last line of that file, you can do something like this:

def read_last_line(file_path):
    f = open(file_path, 'r')
    lines = f.readlines()
    f.close()
    last_line = lines[-1].split()
    return last_line

Notice that lines[-1] is a quick way to get the last element in an array. In this case, that array is an array where each element is a line in your file. I think this may be the important trick you're looking for.

Solution 3:

In this case, I think it is likely that in the real case there are many lines containing values (one for each of many element IDs). To output all the values only, I'd suggest something like:

import re
f_read = open('quad4.txt','r')
f_write=open('quad4.out','w')
str = 'GRID-ID'for line in f_read: 
    m=re.search(str,line)
    if m:
        breakfor line in f_read:   # loop over the rows
    line = line.strip()
    if line:
        vallist = line.split()   # parse the columnsprint (vallist)
        f_write.write("%s\n" % "\t".join(vallist)

This reads the file until it finds a line containing "GRID-ID" then it starts splitting any non-empty line and writing it out in tab-delimited columns. That makes further processing easy. Alternatively, at the third-to-last line of the code above, the values are split into vallist in a convenient form for further processing. Note that in vallist the values are still strings, so if you were to do computations with them, you would need to convert them into int or float values first.

Post a Comment for "Need Help For Extracting Columnar Data"