當前位置:首頁 » 編程語言 » 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)

熱點內容
n皇後演算法 發布:2025-05-20 01:49:15 瀏覽:64
如何配置圖形電腦 發布:2025-05-20 01:47:51 瀏覽:390
及解壓 發布:2025-05-20 01:44:49 瀏覽:415
如何用計算器刷安卓 發布:2025-05-20 01:09:29 瀏覽:576
移動寬頻密碼重置後怎麼辦 發布:2025-05-20 01:02:04 瀏覽:808
php不是內部命令 發布:2025-05-20 00:41:09 瀏覽:97
淘寶圖片上傳用什麼軟體 發布:2025-05-20 00:40:55 瀏覽:346
mysql64位forlinux 發布:2025-05-20 00:37:25 瀏覽:345
工傷輔助器如何配置 發布:2025-05-20 00:25:13 瀏覽:602
opencv存儲圖片 發布:2025-05-20 00:16:10 瀏覽:953