import re import sys inFile = sys.argv[1] outFile = sys.argv[2] with open(inFile, 'r', encoding='utf-8') as inputFile, open(outFile, 'w', encoding='utf-8') as outputFile: for line in inputFile: line = line.rstrip() words = re.findall(r'\w+', line) if len(words) >= 3: # Replace the third word with "xxx" string third_word = words[2] replacement = 'x' * len(third_word) words[2] = replacement # Print the modified line outputFile.write(' '.join(words)+ '\n') else: # If there are less than 3 words, print the original line outputFile.write(line + '\n')