1 首先把源文件读取,循环匹配,匹配到的行做进一步处理,写入目标文件,并换行。
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import warnings warnings.filterwarnings("ignore")
def main(): with open("form.txt", 'rb') as f: try: lines = f.read().splitlines() lines = [line.decode('utf-8-sig') for line in lines] except UnicodeDecodeError: sys.exit() # 打开一个文件 fo = open("to.txt", "w") for line in lines: info = line.split(' ') if len(info) > 0 and info[0].isdigit(): user_config = {} user_config['user_id'] = info[0] if len(info) == 1: fo.write(info[0]) fo.write("\n") else : if len(info) >1: if info[1]=="regstr": fo.write(info[0]) fo.write("\n") # 关闭打开的文件 fo.close() if __name__ == '__main__': main()