我有一個 python 代碼,它打印我想重命名以供以后使用的對象import urllib.request as urlpagina = "https://s3-ifc-coordinador-preprod.s3.amazonaws.com/"pw = url.urlopen(pagina)datos = pw.readlines()print(datos)for i in datos: datos2 = i.decode("utf-8").split("BAEN/") for j in datos2: if j.count(".xlsx") > 0: referencia = j.split("<") #new_referencia = referencia.replace('Ago2019', 'May2020') #print(new_referencia[0]) print(referencia[0])結果:VE02_FIFC_LUZOSORNO_BAENAgo2019.xlsxVE02_FIFC_NORVIND_BAENAgo2019.xlsxVE02_FIFC_POZO_ALMONTE_SOLAR_1_BAENAgo2019.xlsxVE02_FIFC_SAESA_BAENAgo2019.xlsxVE02_FIFC_SAFIRA_ENERGIA_CHILE_BAENAgo2019.xlsxVE02_FIFC_SAN_JUAN_LAP_BAENAgo2019.xlsxVE02_FIFC_SGA_BAENAgo2019.xlsxVE02_FIFC_TACORA_ENERGY_BAENAgo2019.xlsxVE02_FIFC_TRANSELEC_BAENAgo2019.xlsxVE03_FIFC_AES_GENER_BAENAgo2019.xlsxVE03_FIFC_CALAMA_SOLAR_1_BAENAgo2019.xlsxVE03_FIFC_ENGIE_BAENAgo2019.xlsx我需要將“Ago2019”更改為“May2020”明明用replace(),不行。有誰知道該怎么做?我要求的結果VE02_FIFC_LUZOSORNO_BAENMay2020.xlsxVE02_FIFC_NORVIND_BAENMay2020.xlsxVE02_FIFC_POZO_ALMONTE_SOLAR_1_BAENMay2020.xlsxVE02_FIFC_SAESA_BAENMay2020.xlsxVE02_FIFC_SAFIRA_ENERGIA_CHILE_BAENMay2020.xlsxVE02_FIFC_SAN_JUAN_LAP_BAENMay2020.xlsxVE02_FIFC_SGA_BAENMay2020.xlsxVE02_FIFC_TACORA_ENERGY_BAENMay2020.xlsxVE02_FIFC_TRANSELEC_BAENMay2020.xlsxVE03_FIFC_AES_GENER_BAENMay2020.xlsxVE03_FIFC_CALAMA_SOLAR_1_BAENMay2020.xlsxVE03_FIFC_ENGIE_BAENMay2020.xlsx
2 回答

拉丁的傳說
TA貢獻1789條經驗 獲得超8個贊
替換可能沒問題,但您沒有在數組中替換它,而是將它分配給一個您從未使用過的新變量。
嘗試這樣的事情:
referencia = j.split("<")
new_referencia = referencia[0].replace('Ago2019', 'May2020')
print(new_referencia)
添加回答
舉報
0/150
提交
取消