Python:通过引用另一行

时间:2017-09-06 07:20:44

标签: python python-3.x replace find refer

"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"
   ..

我有建筑能源建模的文本文件。

系统(E1 Sys1(PTAC)(G.WNW2))和区域(E1 WNW Perim Zn(G.WNW2))是一对。

我想通过引用空格名称和添加缩写(例如_sys,_zn)来替换系统名称和区域名称,以使其易于理解。

空间名称将位于区域部分的底部。

请您帮我用以下内容替换上述内容?

"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 个答案:

答案 0 :(得分:1)

你可以尝试以下方面:

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)

此代码假定它将在SPACE = xxx的行中运行,否则它将崩溃。希望这有帮助

答案 1 :(得分:1)

使用regex.sub()函数与复杂正则表达式模式的简短解决方案:

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 - 分别指向第1,第2,第3和第4个捕获组(捕获的组是带括号的序列{{1} })

输出:

(...)

答案 2 :(得分:1)

我根据@chngzm提供的方法解决了这个问题。我分享我的代码。这不是一个花哨的代码,但它的工作原理:)

首先,我列出了SPACE名称。我使用tryexcept函数,因为文本文件包含一些空行。

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

然后,根据上面列表中的SPACE名称更改每个SYSTEM和ZONE名称。

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)

这段代码给出了我想要的东西。如果你让我知道任何更好/更好的方法,我将不胜感激。再次感谢! @chngzm