python txt删除重复行
时间: 2023-09-09 22:08:06 浏览: 41
可以使用Python的set()函数来删除txt文件中的重复行,以下是示例代码:
```python
with open('filename.txt','r') as f:
lines = f.readlines()
lines_set = set(lines)
with open('filename.txt','w') as f:
for line in lines_set:
f.write(line)
```
以上代码首先使用`open()`函数读取txt文件中的所有行,然后使用Python的`set()`函数将其转换为一个集合,自动去重。最后再次使用`open()`函数将去重后的结果写回到原文件中。