条件满足时无法更新文件

问题描述 投票:-1回答:2

所以有一个文件,我需要更新以下条件的具体行,我已经在底部添加我的代码问题是我能够打印更新的行不知道创建一个更新的代码文件

if two consecutive lines has CAR words 
at least any of consecutive lines should have '*' in it 
if above two condition is satisfied then 
put '*' two next two lines also ,make sure if a line already has '*' do not add '*' in it 

最后,更新文本文件

例如

请注意文件包含数百个此类实例,需要更正所有

filename4.txt

-------------
* CAR SDFSG FDSFDFDSF   
     CAR FDGDSGGF
bla bla
bla

 CAR SDdsfdfFSG FDSFDFDSF   
     CAR FdffdsDGDSGGF
bla 
bla
----------
expected updated file should be like this

* CAR SDFSG FDSFDFDSF   
*     CAR FDGDSGGF
*bla bla
*bla

 CAR SDdsfdfFSG FDSFDFDSF   
     CAR FdffdsDGDSGGF
bla 
bla
-------------------

这是我的代码

import re
with open("filename4.txt","r+") as file:
      lines = file.readlines()

      x=0

      if re.findall("CAR",lines[x]) and re.findall("CAR",lines[x+1]):
          if re.findall("\*",lines[x]) or re.findall("\*",lines[x+1]):
            if not re.findall("\*",lines[x]):
                lines[x] = "      * "+lines[x]
                print(lines[x])
            if not re.findall("\*",lines[x+1]):
                lines[x+1] = "      * "+lines[x+1]
                print(lines[x+1])
            if not re.findall("\*",lines[x+2]):
                lines[x+2] = "      * "+lines[x+2]
                print(lines[x+2])
            if not re.findall("\*",lines[x+3]):
                lines[x+3] = "      * "+lines[x+3]
                print(lines[x+3])
python python-3.x
2个回答
1
投票

看起来你有多条线路在一起,用空行分隔,然后所有的更改都在整个记录上完成。因此,您需要先将文件分成记录。

一旦你这样做,其余的变得容易。还要注意使用函数来处理不同的任务,并使代码的每个部分负责一个特定的任务(主要是)。

# print one multi-line record
def output_record(lines):
    needs_prefix = is_condition_met(lines)
    for line in lines:
        if needs_prefix and '*' not in line:
            print ('*', line)
        else:
            print (line)
    print()

# determine if this record meets the condition for special handling
def is_condition_met(lines):
    if len(lines) >= 2:
        if 'CAR' in lines[0] and 'CAR' in lines[1]:
            if '*' in lines[0] or '*' in lines[1]:
                return True
    return False

# because I didn't want to do file IO in this example
all_lines = [
    '* CAR SDFSG FDSFDFDSF   ',
    '    CAR FDGDSGGF',
    'bla bla',
    'bla',
    '',
    '    CAR SDdsfdfFSG FDSFDFDSF   ',
    '        CAR FdffdsDGDSGGF',
    'bla ',
    'bla'
]

# parse the whole file into multi-line records
lines_this_record = []
for line in all_lines:
    if line:
        lines_this_record.append(line)
    else:
        output_record(lines_this_record)
        lines_this_record = []

# last record may still need to be printed, 
# if we look for newlines, but there was no blank line at the end of file
if lines_this_record:
    output_record(lines_this_record)

0
投票

谢谢kenney,你的答案对我有很大的帮助,这是替代版本,我更频繁地使用findall

import re


def comment(a,b):
#  print("comment on",a,"condn",b)
  if b == 1:
    list[a+1] = "      * " +    list[a+1]
    list[a+2] = "      * " +    list[a+2]
    list[a+3] = "      * " +    list[a+3]
  if b == 2:
    list[a+2] = "      * " +    list[a+2]
    list[a+3] = "      * " +    list[a+3
                                     ]
    list[a] = "      * "   +    list[a]
  if b == 3:
    print(a)
    list[a+2] = "      * " +    list[a+2]
    list[a+3] = "      * " +    list[a+3]


with open("filename4.txt","r+") as file:
   lines = file.readlines()
   list = lines[:]
   x=0
   for line in lines:
      if re.findall("CAR",lines[x]) and re.findall("CAR",lines[x+1]):
          if re.findall("\*",lines[x]) or re.findall("\*",lines[x+1]):
            if re.findall("\*",lines[x]):
              if not re.findall("\*",lines[x+1]):
                comment(x,1)
            if not re.findall("\*",lines[x]):
              if re.findall("\*",lines[x+1]):
                comment(x,2)

            if re.findall("\*",lines[x]):
              if re.findall("\*",lines[x+1]):
                comment(x,3)


      x=x+1  


with open('path.txt', 'w') as file_handler:
    for item in list:
        file_handler.write("{}".format(item))
© www.soinside.com 2019 - 2024. All rights reserved.