本帖最后由 ivor 于 2020-7-31 15:29 编辑
- #! /usr/bin/env python3
- # coding:utf-8
-
- """
- 取前15字符重复的行的第一行
- """
-
- source_file = '2.txt' # 源文件
- new_file = '3.txt' # 新文件路径
- pre_len = 15
- d = dict()
- with open(source_file,'r',encoding='utf-8') as sou, open(new_file,'w',encoding='utf-8') as new:
- for i in sou:
- if i[:pre_len] not in d:
- d[i[:pre_len]] = i # 前15个字符
- new.write(''.join(d.values()))
复制代码
|