python-在文件内搜索和替换

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

仅当行中的字符串包含某个变量时,我才会逐行解析文件,并用其他标点替换固定的标点(例如,句号“ /”用斜杠“。”)。

Example: Replace only if the string contains Fx

line1: test1/test2
line2: .test + e/y + Fx/var1/var2

Output:
line1: test1/test2
line2: .test + e/y + Fx.var1.var2

我该如何去做?到目前为止的代码,但我知道它不起作用

import os

textToFind = '/'
textToReplace = '.'
sourcepath = os.listdir('InputFiles/')

def lines_that_contain(string, fp):
    return [line for line in fp if string in line]

for file in sourcepath:
    inputFile = 'InputFiles/'+ file
    print('Conversion is ongoing for:' +inputFile)
    with open(inputFile, 'r') as inputFile:
        for line in lines_that_contain("Fx.", inputFile):
            print('found Fx.')
            fileData = fileData.replace(textToFind, textToReplace)
            freq2 = 0
            freq2 = fileData.count(textToFind)

            destinationPath = 'OutputFile/' + file
            with open(destinationPath, 'w') as file:
                file.write(fileData)
                print ('Total %d Record Replaced' %freq2)
        else:
            print('Did not find selected strings')

仅当行中的字符串包含某个变量时,我才会逐行解析文件,并用另一种标点替换固定的标点(例如,句号“ /”用斜杠“。”)。

您的代码有多个问题:

  • fileData在设置之前使用。
  • 您的循环仅在包含“触发”字符串的行上运行,因此您将无法输出未修改的其他行。
  • 如果fileData应该包含到目前为止所读取的所有数据,则替换将影响每一行,无论它是否包含触发器。
  • 输出可能是“已替换0条记录”,后跟“未找到所选字符串”:您正在计算替换后立即出现的替换文本。并且由于您的循环不包含break语句,所以将对else子句进行求值。
  • 您正在为读取的每一行重新创建和写入输出文件。
  • 要解决这些问题,请收集列表中的所有行,如果它们包含触发器,请对其进行修改。阅读完整个文件后,打开输出文件并转储您收集的行。

    import os
    
    textToFind = '/'
    textToReplace = '.'
    trigger = "Fx."
    
    sourcepath = os.listdir('InputFiles/')
    
    for file in sourcepath:
        inputFile = 'InputFiles/'+ file
        print('Conversion is ongoing for:' + inputFile)
        with open(inputFile, 'r') as infile:
            fileData = []
            replacements = 0
            for line in infile:
                if trigger in line:
                    fileData.append(line.replace(textToFind, textToReplace))
                    replacements += 1
                else:
                    fileData.append(line)
    
        destinationPath = 'OutputFile/' + file
        with open(destinationPath, 'w') as outfile:
            # The lines already contain terminating \n characters.
            outfile.write(''.join(fileData))
    
        if replacements > 0:
            print('Total %d Record Replaced' % replacements)
        else:
            print('Did not find selected strings')
    

    由于每一行都是独立处理的,因此您还可以实现流传输版本,在该版本中,您首先打开输入和输出文件,然后一次读取,处理和写入一行。这就是sed程序所做的-在shell中调用sed '/Fx\./ s#/#.#g' inputFile > outputFile对单个文件执行相同的任务。

    python file parsing search replace
    1个回答
    0
    投票

    您的代码有多个问题:

    © www.soinside.com 2019 - 2024. All rights reserved.