您的代码有多个问题:
仅当行中的字符串包含某个变量时,我才会逐行解析文件,并用其他标点替换固定的标点(例如,句号“ /”用斜杠“。”)。
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
应该包含到目前为止所读取的所有数据,则替换将影响每一行,无论它是否包含触发器。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
对单个文件执行相同的任务。
您的代码有多个问题: