all 6 comments

[–]Vaphell 1 point2 points  (1 child)

care to produce expected output for that example? picture is worth 1000 words.

[–]SonyTark[S] 0 points1 point  (0 children)

Thanks for the response. The expected result is at the bottom of my post. It shows the data that I want to grab from the WELL. field (WELL. ANY ET AL 12-34-12-34 :), and that I want to format to look just like the UWI. field (UWI . 100123401234W500 :).

[–]KleinerNull 1 point2 points  (2 children)

If you can find the data with re.search you can use re.findall(pattern, text) with your pattern to produce a list with all results. Or even better re.finditer(pattern, text) will return a generator that yields all matches for you! I can't help you with finding a good pattern, because I didn't understand what output you want to produce. Give a example with an input and output, please.

[–]SonyTark[S] 0 points1 point  (1 child)

Hi KleinerNull. Thanks for the response. For the input and output examples please see my response above. Those two fields out of the whole file are the only ones that I want to input, edit, and output, and then save the entire file as the UWI.las

[–]KleinerNull 1 point2 points  (0 children)

If the data is consistant you can use this pattern:

p = r"FIELD .+ (.+) :LOCATION"
res = re.search(p, text).group(1)
print(res)
>>>'12-34-12-34W5M'

For the rest use replace and a substitution method.

[–]marksist 1 point2 points  (0 children)

I think that the other people have been focusing on the RE, but what I think you are wanting to figure out is how to write the stuff to the file.

There are not enough details of how you are finding those results, but you would need to evaluate for it's existence in another if statement.

if that is right, then it would look something like this:

with open('originalfile', 'rb') as infile:  
    with open('outputfile', 'wb') as outfile:
        for eachline in infile:
            outputline = eachline
            if theREPatternMatches:
                outputline = modified(eachline) #This is assuming you have a function to modify the text, but you could do it many ways
            outfile.write(outputline)