Skip to main content
 首页 » 编程设计

python之如何读取 python 中的特定行并将数据打印到文本文件或控制台窗口

2024年10月24日11birdshome

<分区>

我正在尝试解析文本文件中的行,并打印其中找到的数据值。我需要打印每一行的“DeltaE”和“强度”

到目前为止我的代码:

if ('Duschinsky Tranformation' or 'Calculations of Band Intensities') \ 
    in line: 
    while not 'DeltaE = ' in line: 
        line = next(inputfile) 
deltae = float(line.split()[0]) 

我正在阅读的文本文件:

 ================================================== 
             Duschinsky Transformation 
 ================================================== 
 
 ================================================== 
          Calculations of Band Intensities 
 ================================================== 
 
  Energy of the 0-0 transition:  14394.5813 cm^(-1) 
 
 
 Notes about the overlap integrals description: 
 - DeltaE is the relative energy of the transition (wrt 0-0) 
 - TDMI^2 is the square of the transition dipole moment integral 
 - Intensity is the line intensity 
 
-- To: vibronic fundamental state -- 
 
Initial state: <0| 
Final state: |0> 
  DeltaE =     0.0000 | TDMI^2 =  5.657    , Intensity = 0.5604E+06 
 ........................................ 
 -------------------- 
++ Spectrum progression:   68.60% 
 
-- To: single overtones -- 
 
Initial state: <0| 
Final state: |1^1> 
  DeltaE =    13.5423 | TDMI^2 = 0.6670E-01, Intensity =  6553.     
 ........................................ 
Initial state: <0| 
Final state: |2^1> 
  DeltaE =    17.9918 | TDMI^2 = 0.2693    , Intensity = 0.2668E+05 
 ........................................ 
Initial state: <0| 
Final state: |3^1> 
  DeltaE =    22.4523 | TDMI^2 = 0.4740E-01, Intensity =  4644.     
 ........................................ 
Initial state: <0| 
Final state: |5^1> 
  DeltaE =    26.5963 | TDMI^2 = 0.1649E-01, Intensity =  1633.    

我需要读取 DeltaE 和 Intensity 作为数组。并通过 2 个单独的列将它们写入 txt 文件。就这样

  13.5423      6553 
  17.9918      0.2668E+05 

等等 此外,如果您想查看我正在使用的一些示例,您可以下载解析器文件 http://www.filedropper.com/gaussianparser