编写一个程序,询问用户地址文件的名称和输出文件的名称
Write a program that asks the user for the name of file of addresses and a name of an output file
您的程序应将所有 "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)
问题是它没有打印出任何东西。
请帮忙!
只是为了替换两个thinkgs,这就足够了:
Replace = readfile.replace("NJ", "New Jersey")
Replace = Replace.replace("NY", "New York")
# or
# Replace = readfile.replace("NJ", "New Jersey").replace("NY", "New York")
print(Replace)
这里不需要任何 for 循环。 readfile 已经包含输入文件的全部内容。
要将结果保存到新文件中:
with open("outfile.txt",'w') as f:
f.write(Replace)
类似于:
for i in readfile:
i = i.replace("NY", "New York")
i = i.replace("NJ", "New Jersey")
print (i)
但这不太正确,因为您正在将整个文件读入 readfile。逐行处理文件通常更好
filename = input("Please enter a file name: ")
with open(filename, "r") as readfile:
for i in readfile:
i = i.replace("NY", "New York")
i = i.replace("NJ", "New Jersey")
print (i)
您的程序应将所有 "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)
问题是它没有打印出任何东西。 请帮忙!
只是为了替换两个thinkgs,这就足够了:
Replace = readfile.replace("NJ", "New Jersey")
Replace = Replace.replace("NY", "New York")
# or
# Replace = readfile.replace("NJ", "New Jersey").replace("NY", "New York")
print(Replace)
这里不需要任何 for 循环。 readfile 已经包含输入文件的全部内容。
要将结果保存到新文件中:
with open("outfile.txt",'w') as f:
f.write(Replace)
类似于:
for i in readfile:
i = i.replace("NY", "New York")
i = i.replace("NJ", "New Jersey")
print (i)
但这不太正确,因为您正在将整个文件读入 readfile。逐行处理文件通常更好
filename = input("Please enter a file name: ")
with open(filename, "r") as readfile:
for i in readfile:
i = i.replace("NY", "New York")
i = i.replace("NJ", "New Jersey")
print (i)