Python: find pattern and replace word by referring to another line

107 Views Asked by At
"E1 Sys1 (PTAC) (G.WNW2)" = SYSTEM          
   TYPE             = PTAC
   HEAT-SOURCE      = HEAT-PUMP
   BASEBOARD-SOURCE = NONE
   SIZING-RATIO     = 1.15
   MAX-SUPPLY-T     = 90
   MIN-SUPPLY-T     = 55
   FAN-SCHEDULE     = "S1 Sys1 (PTAC) Fan Sch"
   FAN-CONTROL      = TWO-SPEED
   SUPPLY-STATIC    = 0.5
   SUPPLY-EFF       = 0.33
   COOLING-EIR      = 0.329152
   HEATING-EIR      = 0.34025
   ..
"E1 WNW Perim Zn (G.WNW2)" = ZONE            
   TYPE             = CONDITIONED
   MIN-FLOW-RATIO   = 1
   FLOW/AREA        = 0.5
   OA-FLOW/PER      = 34.6035
   DESIGN-HEAT-T    = 72
   HEAT-TEMP-SCH    = "S1 Sys1 (PTAC) Heat Sch"
   DESIGN-COOL-T    = 75
   COOL-TEMP-SCH    = "S1 Sys1 (PTAC) Cool Sch"
   SIZING-OPTION    = ADJUST-LOADS
   SPACE            = "Unit 100"
   ..

I have this text file for the building energy modeling.

The system (E1 Sys1 (PTAC) (G.WNW2)) and zone (E1 WNW Perim Zn (G.WNW2)) are a pair.

I would like to replace the system name and zone name by referring the space name and adding abbreviations (e.g. _sys, _zn) to make those easy to understand.

The space name will be at the bottom of zone part.

Could you please help me to replace the above with the following?

"Unit 100_sys" = SYSTEM          
   TYPE             = PTAC
   HEAT-SOURCE      = HEAT-PUMP
   BASEBOARD-SOURCE = NONE
   SIZING-RATIO     = 1.15
   MAX-SUPPLY-T     = 90
   MIN-SUPPLY-T     = 55
   FAN-SCHEDULE     = "S1 Sys1 (PTAC) Fan Sch"
   FAN-CONTROL      = TWO-SPEED
   SUPPLY-STATIC    = 0.5
   SUPPLY-EFF       = 0.33
   COOLING-EIR      = 0.329152
   HEATING-EIR      = 0.34025
   ..
"Unit 100_zn" = ZONE            
   TYPE             = CONDITIONED
   MIN-FLOW-RATIO   = 1
   FLOW/AREA        = 0.5
   OA-FLOW/PER      = 34.6035
   DESIGN-HEAT-T    = 72
   HEAT-TEMP-SCH    = "S1 Sys1 (PTAC) Heat Sch"
   DESIGN-COOL-T    = 75
   COOL-TEMP-SCH    = "S1 Sys1 (PTAC) Cool Sch"
   SIZING-OPTION    = ADJUST-LOADS
   SPACE            = "Unit 100"
   ..
3

There are 3 best solutions below

0
On

I solved this problem based on the approach provided by @chngzm. I share my code. This is not a fancy code but it works :)

First, I made a list of SPACE name. I used try and except functions due to the text file includes some blank lines.

file = open("targetfile.txt", "r")  #Read target file
infile = file.readlines()
return_string = ""

# Making list of SPACE names
space = []
for i in range(len(infile)):
    infile[i] = infile[i].split()  #split into the equation parts
    try:
        if infile[i][0] == "SPACE": #found space element
            space.append(" ".join(map(str,infile[i][2:]))) #take the name which is the third element and after
    except:
        continue
space = [x.strip('"') for x in space] #take off quotation marks

and then, change each SYSTEM and ZONE name according to SPACE name from the list made above.

for i in range(len(infile)):
    try:
        if infile[i][-1] == "SYSTEM":
            del infile[i][:-2] 
            infile[i].insert(0,"\""+space[0] + "_sys"+"\"")
        elif infile[i][-1] == "ZONE":
            del infile[i][:-2] 
            infile[i].insert(0,"\""+space[0] + "_zn"+"\"")
            del space[0] #remove used SPACE name
        return_string += (" ".join(infile[i]) + "\n") #recreating the file
    except:
        continue
print (return_string)

This code gave what I exactly wanted. I would appreciate it if you let me know any better/fancy approaches. Thanks again! @chngzm

2
On

you can try something along the lines of:

file = open(file_name, "r")
infile = file.readlines()
return_string = ""

for i in range(len(infile)):
    infile[i] = infile[i].split()  #split into the equation parts
    if infile[i][0] == "SPACE": #found space element
        space = ' '.join(infile[i][2:])  #take the name which is the third element and after
for i in range(len(infile)):
    if infile[i][2] == "ZONE":
        infile[i][0] = space + "_zn"
    elif infile[i][2] == "SYSTEM":
        infile[i][0] = space + "_sys"
    return_string += (" ".join(infile[i]) + "\n") #recreating the file

print (return_string)

This code assumes that it will run into a line where SPACE = xxx, else it will crash. Hope this helps

2
On

Short solution using regex.sub() function with complex regex pattern:

import re

with open('yourfile.txt', 'r') as f:
    pat = re.compile('"[^"]+"( = SYSTEM[\s\S]+)"[^"]+"( = ZONE[\s\S]+)(SPACE\s*= )"([^"]+)"')
    content = pat.sub('"\\4_sys"\\1"\\4_zn"\\2\\3"\\4"', f.read(), re.M)
    print(content)
  • \\1, \\2, \\3, \\4 - point to the 1st, 2nd, 3rd and 4th captured group respectively (captured group is parenthesized sequence (...))

The output:

"Unit 100_sys" = SYSTEM          
   TYPE             = PTAC
   HEAT-SOURCE      = HEAT-PUMP
   BASEBOARD-SOURCE = NONE
   SIZING-RATIO     = 1.15
   MAX-SUPPLY-T     = 90
   MIN-SUPPLY-T     = 55
   FAN-SCHEDULE     = "S1 Sys1 (PTAC) Fan Sch"
   FAN-CONTROL      = TWO-SPEED
   SUPPLY-STATIC    = 0.5
   SUPPLY-EFF       = 0.33
   COOLING-EIR      = 0.329152
   HEATING-EIR      = 0.34025
   ..
"Unit 100_zn" = ZONE            
   TYPE             = CONDITIONED
   MIN-FLOW-RATIO   = 1
   FLOW/AREA        = 0.5
   OA-FLOW/PER      = 34.6035
   DESIGN-HEAT-T    = 72
   HEAT-TEMP-SCH    = "S1 Sys1 (PTAC) Heat Sch"
   DESIGN-COOL-T    = 75
   COOL-TEMP-SCH    = "S1 Sys1 (PTAC) Cool Sch"
   SIZING-OPTION    = ADJUST-LOADS
   SPACE            = "Unit 100"
   ..