作者CSTing (海蒂)
看板Python
标题[问题] 如何将.py档的输出内容写出到.txt档案?
时间Fri Jan 8 12:45:04 2010
我欲读入的方程式如下:
def printMultiplierTable():
maxMultiplier = 10
n1 = 1
while n1 < maxMultiplier:
n2 = 1
s = ""
while n2 < maxMultiplier:
n3 = n1*n2
if n3<= 9:
s = s + " " + str(n1) + "*" + str(n2) + "=" + " " + str( n1*n2 )
n2 = n2 + 1
else:
s = s + " " + str(n1) + "*" + str(n2) + "=" + str( n1*n2 )
n2 = n2 + 1
print s
n1 = n1 + 1
if __name__=="__main__":
printMultiplierTable()
其实这是一个写出九九乘法表的小程式。
如果我要把程式中的九九乘法表写到另一个txt档
要如何做?
fout = open('MultiplierTable.txt', 'wt')
fin = open('??') --->这里我不知道要放什麽
for line in fin:
lines = line.strip()
for line in lines:
fout.write(line) ## 写出资料
fout.write( "\n" ) ## 写出换行字元
# 档案有打开就要有关上, 以让资料写出去
fout.close()
谢谢各位指教:)
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.217.78
1F:→ a761007:你的input应该没有要从档案读取吧,所以该行去除掉就好了 01/08 14:39
2F:→ aquarianboy:以标题来看,只要你输出的内容是正确的,用个 > 就行了 01/08 15:19
3F:→ aquarianboy:python your_python.py > your_textfile.txt 01/08 15:20