Regex re.sub list in a file

11,288

Solution 1

"0000-22N-06W-01"
"0000-22N-06W-02"
"0000-22N-06W-03"
"0000-22N-06W-04"

import re
output = open("output.txt","w")
input = open("input.txt")

for line in input:
    output.write(re.sub(r'^(.{4})-(.{3})-(.{3})-(.{2})$', r'\1-\4-\2-\3', line))

input.close()
output.close()

NOTE: If you actually have " in your data then you should change your regular expression to this one:

^"(.{4})-(.{4})-(.{3})-(.{3})"$

Regex101 Demo

Solution 2

If you still want to use .read(), try this:

import re
output = open("output.txt","w")
input = open("input.txt").read()

output.write(re.sub(r'^(.{4})(.{4})(.{4})(.{3})$',
                    r'\1\4\2\3', 
                    input, 
                    flags=re.M))

output.close()
Share:
11,288
George
Author by

George

GIS analyst for the City of Enid Oklahoma

Updated on June 14, 2022

Comments

  • George
    George almost 2 years

    I have a text list that will re.sub fine with some like: re.sub('0000', '1111',data).

    A replace pattern ^(.{4})(.{4})(.{3})(.{3}) with \1\4\2\3 for one input in the shell works fine also. However, my attempt to use this pattern in a list gives me an undesirable result on the first row and never replace the latters. What am I missing here?

    "0000-22N-06W-01"
    "0000-22N-06W-02"
    "0000-22N-06W-03"
    "0000-22N-06W-04"
    
    import re
    o = open("output.txt","w")
    data = open("input.txt").read()
    o.write(re.sub(r'^(.{4})(.{4})(.{3})(.{3})', r'\1\4\2\3',data))
    o.close()
    
  • Jerry
    Jerry over 10 years
  • Ibrahim Najjar
    Ibrahim Najjar over 10 years
    Nice one @Jerry. I don't work with Python, I copied code from here and there, modified it a bit and then my C# habits came into the scene :).
  • George
    George over 10 years
    The output should write 0000-01-22N-6W, 0000-02-22N-6W, 0000-03-22N-6W, 0000-04-22N-6W as the pattern is \1\4\2\3. I'm still getting the same input.
  • Ibrahim Najjar
    Ibrahim Najjar over 10 years
    @George I have included a demo in my answer, check it and I will see what might be causing your issue.
  • Ibrahim Najjar
    Ibrahim Najjar over 10 years
    @George I have just tested the code on my machine and everything is working pretty well.
  • eyquem
    eyquem over 10 years
    Ahem. I should have read your answer more cmosely before writing mine, and you should make more immediately readable your answer.
  • George
    George over 10 years
    Your first sample was fine. I just need to be careful about cleaning my sample script. My old example (still active), was still taunting me. Your the best Jerry.
  • Jerry
    Jerry over 10 years
    @George I'm Jerry, and he's Sniffer ^^