【问题标题】:python logic/code to skip few lines by comparing stringpython逻辑/代码通过比较字符串跳过几行
【发布时间】:2022-09-27 12:45:42
【问题描述】:

我有一个文件并包含一些数据

 Net Name:  VDD
 Pin Name:  VDD
 Instance Name:  ld_slice_logic_wrap_inst/ld_slice_logic_top_inst/ld_clk_gen_top_inst/ld_wdqs_clk_gen

 Layer/Via#  PGResistor  Resistance(cumulative) Ohm  Voltage_Drop(cumulative) V  Length 
 VSRC       N 0.0000e+00 0.0000e+00 3.7865e-03 3.7865e-03  0  144.43150,550.00000   
 AP         N 1.0000e-06 1.0000e-06 0.0000e+00 3.7865e-03  0.000100000001  144.43150,550
 AP         N 3.1474e-03 3.1484e-03 5.6028e-06 3.7921e-03  6.8432998  144.43200,550.0000
 AP         N 1.0000e-06 3.1494e-03 0.0000e+00 3.7921e-03  0.000200000003  144.43200,543
 AP         N 1.9433e-03 5.0927e-03 1.1802e-05 3.8039e-03  4.1663999  144.43200,543.1565
 AP         N 2.5653e-03 7.6580e-03 1.7583e-05 3.8215e-03  5.5001001  144.43200,538.9900
 AP         N 1.0137e-02 1.7795e-02 7.0572e-05 3.8921e-03  8.851  144.43200,533.49000   
 AP         N 2.1164e-02 3.8959e-02 1.5050e-04 4.0426e-03  16.0295  144.43200,524.63900 
 AP         N 9.9838e-07 3.8960e-02 0.0000e+00 4.0426e-03  0.054  128.40250,524.63900   
 RV         N 3.3405e-02 7.2365e-02 2.3925e-04 4.2818e-03  2.7  128.40250,524.69300   
 M15        N 9.9838e-07 7.2366e-02 0.0000e+00 4.2818e-03  0.6985  128.40250,524.69300  
 M15        N 1.0580e-01 1.7817e-01 4.3488e-04 4.7167e-03  14.947  127.70400,524.69300  
 VIA14      N 8.8155e-03 1.8698e-01 5.1737e-05 4.7684e-03  0.324  127.70400,539.64000   
 M14        N 3.5033e-02 2.2201e-01 1.1855e-04 4.8870e-03  4.96  127.70400,539.64000   
 M14        N 1.6951e-01 3.9153e-01 1.3857e-03 6.2727e-03  24  132.66400,539.64000   
 M9         N 9.5367e-07 6.2670e+00 0.0000e+00 7.4605e-03  0.26  187.49200,516.86300 
      
 Layer/Via  Resistance(Ohm)  Voltage_Drop(V)  R_Cumulative(Ohm)  Vdrop_Cummulative(V) 
 VSRC        0.0000e+00  3.7865e-03  0.0000e+00  3.7865e-03 
 AP          3.8960e-02  2.5606e-04  3.8960e-02  4.0426e-03 
 RV          3.3405e-02  2.3925e-04  7.2365e-02  4.2818e-03 
 M15         1.0580e-01  4.3488e-04  1.7817e-01  4.7167e-03 
 VIA14       8.8155e-03  5.1737e-05  1.8698e-01  4.7684e-03 
 M9          2.2392e+00  5.8532e-05  6.2670e+00  7.4605e-03 

 Net Name:  VSS
 Pin Name:  VSS
 Instance Name:  ld_slice_logic_wrap_inst/ld_slice_logic_top_inst/ld_clk_gen_top_inst/ld_wdqs_clk_gen

 Layer/Via#  PGResistor  Resistance(cumulative) Ohm  Voltage_Drop(cumulative) V  Length 
 VSRC       N 0.0000e+00 0.0000e+00 3.7865e-03 3.7865e-03  0  144.43150,550.00000   
 AP         N 1.0000e-06 1.0000e-06 0.0000e+00 3.7865e-03  0.000100000001  144.43150,550
 AP         N 3.1474e-03 3.1484e-03 5.6028e-06 3.7921e-03  6.8432998  144.43200,550.0000
 AP         N 1.0000e-06 3.1494e-03 0.0000e+00 3.7921e-03  0.000200000003  144.43200,543
 AP         N 1.9433e-03 5.0927e-03 1.1802e-05 3.8039e-03  4.1663999  144.43200,543.1565
 AP         N 2.5653e-03 7.6580e-03 1.7583e-05 3.8215e-03  5.5001001  144.43200,538.9900
 AP         N 1.0137e-02 1.7795e-02 7.0572e-05 3.8921e-03  8.851  144.43200,533.49000   
 AP         N 2.1164e-02 3.8959e-02 1.5050e-04 4.0426e-03  16.0295  144.43200,524.63900 
 AP         N 9.9838e-07 3.8960e-02 0.0000e+00 4.0426e-03  0.054  128.40250,524.63900   
 RV         N 3.3405e-02 7.2365e-02 2.3925e-04 4.2818e-03  2.7  128.40250,524.69300   
 M15        N 9.9838e-07 7.2366e-02 0.0000e+00 4.2818e-03  0.6985  128.40250,524.69300  
 M15        N 1.0580e-01 1.7817e-01 4.3488e-04 4.7167e-03  14.947  127.70400,524.69300  
 VIA14      N 8.8155e-03 1.8698e-01 5.1737e-05 4.7684e-03  0.324  127.70400,539.64000   
 M14        N 3.5033e-02 2.2201e-01 1.1855e-04 4.8870e-03  4.96  127.70400,539.64000   
 M14        N 1.6951e-01 3.9153e-01 1.3857e-03 6.2727e-03  24  132.66400,539.64000   
 M9         N 9.5367e-07 6.2670e+00 0.0000e+00 7.4605e-03  0.26  187.49200,516.86300 
      
 Layer/Via  Resistance(Ohm)  Voltage_Drop(V)  R_Cumulative(Ohm)  Vdrop_Cummulative(V) 
 VSRC        0.0000e+00  3.7865e-03  0.0000e+00  3.7865e-03 
 AP          3.8960e-02  2.5606e-04  3.8960e-02  4.0426e-03 
 RV          3.3405e-02  2.3925e-04  7.2365e-02  4.2818e-03 
 M15         1.0580e-01  4.3488e-04  1.7817e-01  4.7167e-03 
 VIA14       8.8155e-03  5.1737e-05  1.8698e-01  4.7684e-03 
 M9          2.2392e+00  5.8532e-05  6.2670e+00  7.4655e-03 

文件数据将重复网名引脚名称作为VDDA,VSS,VSSA....

我想跳过数据之间层/过孔#层/过孔我只想考虑M9具有最后一个值的层/过孔之后的数据\" 例如:7.4605e-03 \"

我试过的逻辑

def readPinFile(filename): 

    result = None
    with open(filename, \"r\") as file:   

        result = {}
        lastPin = None        
        for line in file:
            lines = line.strip()
            
            if lines[:3] == \"Net\": 
                Net_Name = lines.split(\" \")[-1]               
                result[lines] = {\"Pin_Name\": None, \"M9\": None}
            
            if lines[:3] == \"Pin\":
                result[lines][\"Pin_Name\"] = lines.split(\" \")[1]
               
            if lines[:2] == \"M9\":
                result[Net_Name][\"M9\"] = lines.split(\" \")[-1]

    return result
 
readPinFile(\"inst_1.txt\")

预期产出

{\'Pin_Name\': \'VDD\', \'Net_Name\': \'VSS\', \'M9\': \'7.4605e-03\'}
{\'Pin_Name\': \'VSS\', \'Net_Name\': \'VSS\', \'M9\': \'7.4655e-03\'}

到了这个阶段后,我一头雾水,茫然地继续。 谁能指导我摆脱困境。

  • 你能提供预期的输出吗?
  • 您的文件有多少个部分?您是否只对跳过第一部分感兴趣?
  • @SB 实际上有 350 多个部分,每次我想跳过第一部分并在变量上写入数据时。
  • 如果您检查了答案,请告诉我它与预期行为有何不同。

标签: python


【解决方案1】:

对于net_namepin_name,您不需要在每次迭代中都添加if 语句。文件对象是一个迭代器,所以你可以在它们上调用next

我假设您只想跳过第一部分,所以我使用了一个标志来跳过两个 "Layer/Via" 之间的行:

def read_pin_file(filename):
    with open(filename, "r", encoding="utf-8") as f:
        # get infromation from fist two lines
        net_name = next(f).strip().rsplit(maxsplit=1)[1]
        pin_name = next(f).strip().rsplit(maxsplit=1)[1]

        # This loop is for skipping that secction
        flag_count = 0
        for line in f:
            if line.strip().startswith("Layer/Via"):
                flag_count += 1
            if flag_count == 2:
                # We're on section two.
                break

        for line in f:
            line = line.strip()
            if line.startswith("M9"):
                return {
                    "Pin_Name": pin_name,
                    "Net_Name": net_name,
                    "M9": line.rsplit(maxsplit=1)[1],
                }

print(read_pin_file("inst_1.txt"))

输出:

{'Pin_Name': 'VDD', 'Net_Name': 'VDD', 'M9': '7.4605e-03'}

【讨论】:

  • 第 4 行和第 5 行因“列表索引超出范围”而出错。你能解释一下吗:encoding="utf-8" 以及行号 4&5
  • @SanthoshNayakD。你会得到例外,因为我假设 Net Name: VDD 行和 Pin Name: VDD 行分别出现在第 1 行和第 2 行。这就是您的文本文件所指示的。由于它们至少包含两个单词,因此您可以从拆分结果中获得索引 1。
  • @SanthoshNayakD。 encoding="utf-8" 在这里并不重要,我只是将其添加为一种习惯。它告诉 Python 应该使用哪种编码来解析文件。
  • @SanthoshNayakD。 next() 为您提供下一行,rsplit,从右侧拆分行。
  • 如果相同的数据多次出现,我想改变什么?
猜你喜欢
  • 2010-12-24
  • 1970-01-01
  • 2015-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 2018-05-18
相关资源
最近更新 更多