A. python列表,如何批量查找替換
批量替換列表的字元a為b
>>>list_=['1','a','3','a']
>>>rep_list=map(lambdax:[x,'b'][x=='a'],list_)
>>>rep_list
['1','b','3','b']
>>>
B. 怎麼用python批量修改一組文件名
有個工具叫 bulkrenameutility,使用其正則替換能達到你的要求。
Python的話,也可以用正則完成,或者獲取文件名後用切片也能完成文件名前後的交換。
C. Python如何將文件夾中的所有txt文件的內容替換
下面是我寫的,供參考:
import os
path = r'D:Desktope'
files = list(filter(lambda file:file[-4:]=='.txt',os.listdir(path)))
for file in files:
with open(path+os.sep+file,'r+') as f:
data = f.read()
data.replace('wo','我')
f.write(data)
D. 如何用python的re.sub( )方法進行「多處」替換
正則表達式里 或 是 |
importre
s0='BOYandGIRL'
s1=re.sub(r'BOY|GIRL','HUMAN',s0)
prints1
#HUMANandHUMAN
E. python中如何批量替換字母+數字為浮點數或整數類型
給你個思路:
1. 通過正則表達式,來提取你要求的數據,前面兩個字母,後四位數字。
2. 對提取的數據進行分離出字母和數字兩部分。
3. 將提取的數字部分進行轉換
4. 然後在將字母和轉換後的數字進行拼接,這步可有可無。。。
當然還有個簡單的方法,上面的思路是清晰的,但是相對來說是繁瑣的,比較low。
希望能幫到你。。。。。。
F. python如何批量修改列表元素
list = ['一班張三', '二班李明', '二班張麗麗', '四班王強', '一班張志華']
list = ['樹人小學' + re.sub('^\w\w', '', x) for x in list]
print(list)
G. python如何實現批量變更文件名
b = a.replace("文件夾","文件") # 這一句的效果是將-替換為空
# 這里可以再加個判斷是否有「文件」兩字,沒有給他加上即可
if b.find("文件") == -1:
b = "文件" + b
H. 求助,怎麼運用Python腳本批量替換mxd文件中的文本
importarcpy,string,os
#
Path=arcpy.GetParameterAsText(0)
oldText=arcpy.GetParameterAsText(1)
newText=arcpy.GetParameterAsText(2)
case=arcpy.GetParameter(3)
exact=arcpy.GetParameter(4)
outputMXD=arcpy.GetParameterAsText(5)
try:
#Referentthemapdocument
mxd=arcpy.mapping.MapDocument(mxdPath)
#Findallpagelayouttextelements
forelminarcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT"):
ifexact:
ifcase:
ifoldText==elm.text:
elmText=elm.text.replace(oldText,newText)
elm.text=elmText
else:
ifoldText.upper()==elm.text.upper():
elmText=elm.text.upper().replace(oldText,newText)
elm.text=elmText
else:
ifcase:
ifoldTextinelm.text:
elmText=elm.text.replace(oldText,newText)
elm.text=elmText
else:
ifoldText.upper()inelm.text.upper():
elmText=elm.text.upper().replace(oldText,newText)
elm.text=elmText
mxd.saveACopy(outputMXD)
delmxd
exceptException,e:
importtraceback
map(arcpy.AddError,traceback.format_exc().split(" "))
arcpy.AddError(str(e))
I. 如何在Python 批量修改HTML文件里的span內容
因為你的html不是合法的xml格式,標簽沒有成對出現,只能用html解析器
from bs4 import BeautifulSoup
s = """
</span><span style= 'font-size:12.0pt;color:#CC3399'>714659079qqcom 2014/09/10 10:14</span></p></div>
soup = BeautifulSoup(s, "html.parser")
print soup
print soup.get_text()
J. 如何使用Python批量修改文件中有規律的字元串
python中快速進行多個字元替換的方法小結
先給出結論:
要替換的字元數量不多時,可以直接鏈式replace()方法進行替換,效率非常高;
如果要替換的字元數量較多,則推薦在
for
循環中調用 replace() 進行替換。
可行的方法:
1.
鏈式replace()
?
1
string.replace().replace()
1.x
在
for循環
中調用replace() 「在要替換的字元較多時」
2.
使用string.maketrans
3.
先
re.compile
然後
re.sub