當前位置:首頁 » 編程語言 » python字元替換

python字元替換

發布時間: 2022-01-31 14:26:10

python 字元串替換

str='aaaaaaaaaa'
ls=list(str)
ls[2]='0'
ls[3]='0'
ls[4]='0'
ls[5]='0'
ls[6]='0'
new_str=''.join(ls)#'aa00000aaa'

❷ Python如何替換list中的字元

利用re庫進行正則表達替換,python3.4中

importre

a=["123","456","789"]
d={"B":"2","E":"5","H":"8"}
b=",".join(a)
print(b)

foriind.keys():
b=re.sub(d[i],i,b)
print(b.split(","))

❸ python中如何替換字元串

replace()實現字元串替換

使用案例

❹ python中文字元串替換字元

a.replace("|","\n")應該改成a=a.replace("|","\n")
因為a.replace()並沒有改變a的值,只是將從a讀取出來的內容改變了

❺ Python字元串替換replace簡單

re.sub(r'[\n\r\t "]',"",string)

❻ 關於Python,怎樣才能把字元串中某一個重復的字元都替換

#-*-coding:utf-8-*-
numbers='0123456789'
a="r1.5ml.in.9it"
file=[]
foriina:
file.append(i)
printfile

foridx,iinenumerate(file):
#先前是因為第二次index('.')的時候,獲取的還是第一個點
ifi=='.'andi!=file[-1]andfile[idx+1]innumbers:
file[idx]='*'
else:
i=i
print(file)

如果解決了您的問題請採納!
如果未解決請繼續追問

❼ python 查找字元串並將其替換

f1=open('J:/wenjian/1/1.txt','r')
for line in f1
你這里是不是少了點什麼,f1隻是文件句柄,需要執行讀操作才能遍歷,
調用readlines()

確實有更好的代碼,那就是使用re.sub,它同時包含了查找和替換兩步的操作,
而不是像你寫的那樣的字元串比較性能那麼低

❽ python中如何對多個字元快速替換

python中快速進行多個字元替換的方法小結

先給出結論:

  • 要替換的字元數量不多時,可以直接鏈式replace()方法進行替換,效率非常高;

  • 如果要替換的字元數量較多,則推薦在 for 循環中調用replace()進行替換。

  • 可行的方法:

    1. 鏈式replace()

    ?

    1
  • string.replace().replace()
  • 1.x 在for循環中調用replace()「在要替換的字元較多時」

    2. 使用string.maketrans

    3. 先 re.compile 然後 re.sub

❾ python字元替換replace

>>> aa='up(1)'
>>> import re
>>> re.sub(r'\(\d\)','',aa)
'up'

❿ python字元串替換問題

簡單點,用正則的話,建議用re.sub

importre
a='!x'
b=re.sub('!','not',a)
print(b)

熱點內容
打包sql資料庫 發布:2025-07-12 16:19:27 瀏覽:794
php日誌查看 發布:2025-07-12 16:12:10 瀏覽:212
ftp目錄映射為本地盤符 發布:2025-07-12 16:06:59 瀏覽:643
nas存儲百科 發布:2025-07-12 16:03:17 瀏覽:124
python的sort函數 發布:2025-07-12 15:53:21 瀏覽:48
ensp伺服器怎麼設置web根目錄 發布:2025-07-12 15:47:56 瀏覽:284
安卓怎麼設置二卡發信息 發布:2025-07-12 15:43:50 瀏覽:743
如何看到無線密碼 發布:2025-07-12 15:43:13 瀏覽:675
好網址可緩存 發布:2025-07-12 15:36:07 瀏覽:252
centos安裝php52 發布:2025-07-12 15:14:19 瀏覽:298