如果for循环中的语句在文件中的行上

时间:2017-11-23 22:34:00

标签: python for-loop if-statement lines python-textprocessing

我有一个文件data.txt

./path1
 * WITH LDLDLDLDLDLD                 *
  KDKDKDKDKD
  LDLDLDLDLDLDLDLD
  LDFLFLFLFLFLFLF
['-2.6993']
['-2.6983']
['-2.4490']
  LSLSLSLSLSL
['-2.6993']
['-2.6983']
['-2.4490']
  KKKGKGKGKGKGKGKG
['-79.7549']
  LDLDLDLDLDLDLDLDL
['-126.6208']
['-93.9881']
  KDKDKDKDKDKDKDKD
['-156.9296']
['-135.3548']
  LDLDLDLDDLDDLDLDLD
['-178.3941']
['-162.8602']
['-42.7064']
  KDKDKDKDKDLDLDLDLDLD
['-193.3335']
['-181.9782']
['-68.6555']

./path2
 * WITH DLLDLDLDLDLLDLD                 *
  LDLDLDLDLDLDLD
  BEBEBEBEBEBEL
  LSLSLSLSLSLSL
['-2.6993']
['-2.6983']
['-2.4490']
  OSOSOSOSOSOSOSOS
['-2.6993']
['-2.6983']
['-2.4490']
  KDKDKDKDKDKDKDKDKD
['-156.9296']
['-135.3548']
  MDMDMDMDMDMDDMDM
['-178.3941']
['-162.8602']
['-42.7064']
  KFKFKFKFPKLDLDLD
['-193.3335']
['-181.9782']
['-105.4751']
['-96.2342']

我想从中打印path以及该路径上的负值。

以下代码实现了这一目标:

import re
import os
import numpy as np

f = open('data.txt', 'r')
All_aux = []

for line in f:
         if re.match(r"^\.", line):
          print line

         if re.match(r"^\[", line):

                 target2 = line.translate(None, "[]'',")    
                 aux = target2.split()
                 All_aux.append(aux)
                 flat_list = [item for sublist in All_aux for item in sublist]

print 'len(negatives) = ' , len(flat_list)

但打印的信息如下:

./path1

./path2

len(negatives) =  32

匹配第一个if re.match(r"^\.", line):后,它会打印该行,但不会打印前17个负值。相反,此值将被保存并汇总到第二条路径上找到的15个负值。

我想获得以下内容:

./path1

len(negatives) =  17

./path2

len(negatives) =  15

有没有办法实现这个目标?

1 个答案:

答案 0 :(得分:2)

这就是我对评论的意思。我也做了一些其他改进,例如使用字符串方法,它通常比正则表达式更简单,更有效。

经过与@tripleee的一点思考和讨论之后,我已经放弃了content,因为你所做的只是计算长度。

我已评论过,但如果您不明白,请询问:

flat_list

这给出了:

# None of the imports are required
# We only need a count
negatives = 0

# Previously you were not closing the file
# This closes it automagically
with open('data.txt', 'r') as f:
    for line in f:
        # No need for a regular expression here
        if line.startswith("./"):
            if negatives:
                print 'len(negatives) = ' , negatives, '\n'
                negatives = 0
            print line

        # Used "else if" since both `startswith` can't be true
        elif line.startswith("["):
            target2 = line.translate(None, "[]'',")
            # simplified
            negatives += len(target2.split())

if negatives:
    print 'len(negatives) = ' , negatives