编写一个 Python 程序,读取名为“input.txt”的文本文件的内容,删除重复行,并将唯一行写入名为“output.txt”的新文件。如果行具有相同的内容,则视为重复行,无论前导空格或尾随空格如何。 (使用上下文管理器)
例如,如果“input.txt”包含: '你好世界! Python 很有趣。 Python 很有趣。 Python 很有趣。'
运行程序后,“output.txt”应包含:
'你好,世界! Python 很有趣。'
正确处理错误
对于上述问题我正在使用以下解决方案
`def remove_duplicates(input_file, output_file):
try:
with open(input_file, 'r') as in_file:
lines = in_file.readlines()
unique_lines = set(line.strip() for line in lines if line.strip())
with open(output_file, 'w') as out_file:
for line in unique_lines:
out_file.write(f"{line}\n")
print("Unique lines written to output.txt successfully.")
except FileNotFoundError:
print("Error: input.txt not found.")
except PermissionError:
print("Error: Permission denied while opening the file.")
except Exception as e:
print(f"An error occurred: {e}")
# Usage
remove_duplicates('input.txt', 'output.txt')`
但是它给出了以下输出 '你好世界! Python 很有趣。 Python 很有趣。'
有人可以告诉我的输出有什么问题并请修改我的代码吗?
strip()
方法用于在将每行添加到 unique_lines 集合之前删除每行的前导和尾随空白。
def remove_duplicates(input_file, output_file):
try:
with open(input_file, 'r') as in_file:
lines = in_file.readlines()
# Use a set to store unique lines after stripping leading and trailing whitespace
unique_lines = set(line.strip() for line in lines)
with open(output_file, 'w') as out_file:
for line in unique_lines:
out_file.write(f"{line}\n")
print("Unique lines written to output.txt successfully.")
except FileNotFoundError:
print("Error: input.txt not found.")
except PermissionError:
print("Error: Permission denied while opening the file.")
except Exception as e:
print(f"An error occurred: {e}")
# Usage
remove_duplicates('input.txt', 'output.txt')