您的程序应该将“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