您的程序應該將「NY」的所有出現次數替換爲「New York」,將所有出現的「NJ」替換爲「New Jersey編寫一個程序,要求用戶輸入地址文件的名稱和輸出文件的名稱
例如,如果你的文件replace.txt
包含:
from wikipedia:
NY-NJ-CT Tri-State Area
The NY metropolitan area includes the most populous city in the US
(NY City); counties comprising Long Island and the Mid- and Lower Hudson
Valley in the state of New York.
輸出必須是:
from wikipedia:
New York-New Jersey-CT Tri-State Area
The New York metropolitan area includes the most populous city in the United
States (New York City); counties comprising Long Island and the Mid- and
Lower Hudson Valley in the state of New York.
我盡力了,這裏是我的程序
filename = input("Please enter a file name: ")
openfile = open(filename, "r")
readfile = openfile.read()
for i in readfile:
for string in i.replace("NY", "New York"):
Replace = string.replace("NJ", "New Jersey")
print(Replace)
問題是它沒有打印出任何東西。 請幫助!
不適用於新澤西州! – kunjani 2015-03-24 23:46:24
只換NY! – kunjani 2015-03-24 23:46:35
現在有效。你在我編輯安納塞爾時檢查過。 – Marcin 2015-03-24 23:46:48