python查找字符串内的某个字符,进行操作

时间:2022-12-02 15:54:29

找出每一行中的某个字段来进行识别,并在后面添加此标记的内容

# -*- coding: utf-8 -*-
''' 查找每个字符串中是否包含某个字符,在该字符串后添加东西 或者使用replace方法进行替换 '''
import re
def fenhang(infile,outfile):

    infopen = open(infile,'r',encoding='utf-8')
    outopen = open(outfile,'w',encoding='utf-8')
    lines = infopen.readlines()
    db = []
    for line in lines:
        db.append(line)
    for i in range(0,len(db)):
        sett = db[i].strip()
        if 'f' in sett:
            outopen.write(sett+' B-PER'+'\n')
        elif 'z' in sett:
            outopen.write(sett+' B-LOC'+'\n')
        elif 'a' in sett:
            outopen.write(sett+' B-ORG'+'\n')
        else:
            outopen.write(sett + ' O'+'\n')
    infopen.close()
    outopen.close()
fenhang("C:/Users/Administrator/Desktop/new2.txt","C:/Users/Administrator/Desktop/new.en")