python查詢字串內的某個字元,進行操作

2021-08-17 12:16:51 字數 841 閱讀 4236

找出每一行中的某個欄位來進行識別,並在後面新增此標記的內容

# -*- 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:

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")

查詢字串

在乙個主串中查詢相應的子串,如 abcdwoshidef 中查詢 woshi 方法 該實現的方法是最簡單的模式匹配方法,時間複雜度較高 include iostream using namespace std int searchstring const char str1,const char s...

查詢字串

qstring startwith 判斷乙個字串是否以某個字串開頭,引數 字串,大小寫敏感 qstring str welcome to you str.startswith welcome qt casesensitive 返回true str.startswith you qt casesens...

查詢字串

本身不難,寫到這裡只是乙個備忘錄的作用。假定linux系統中有乙個目錄,其中遞迴的存在若干子目錄。現在需要在這些目錄的檔案中尋找乙個字串marvel。我看到的方法是 find type f exec grep marvel 可是死活就是錯誤 find missing argument to exec...