【发布时间】:2016-02-19 06:09:17
【问题描述】:
下面是我的代码,我正在处理的问题是让我的程序的输出“写入一个文件,该文件的名称是通过将字符串 _output 附加到输入文件名来获得的”。
这样做的正确方法是什么?
fileName = raw_input('Enter the HTML file name:') + '.html'
f = open(fileName, 'r')
myList = f.readlines()
for i in range(0, len(myList)):
toString = ''.join(myList)
newString = toString.replace('<span>', '')
newString = newString.replace('</span>', '')
print newString #testing the output
f.close()
这是修改后的代码。像这样?
fileName = raw_input('Enter the HTML file name:') + '.html'
f = open(fileName, 'r')
fnew = open(fileName, 'w')
myList = f.readlines()
for i in range(0, len(myList)):
toString = ''.join(myList)
newString = toString.replace('<span>', '')
newString = newString.replace('</span>', '')
fnew.write(newString)
f.close()
【问题讨论】:
-
您正在读取而不是写入文件。看看这个:stackoverflow.com/questions/6159900/…
-
您的问题是如何连接字符串(只需使用
+)还是如何写入文件? -
我没有得到写入文件的代码,请提供。
-
您必须以
w模式打开另一个文件。并在该文件对象上调用write。我们可以提供解决方案,但首先尝试您自己,并在此处提供代码。这样我们就可以在写入文件中解决您的问题。 -
以
open(filename + '_output', 'w')开头
标签: python