此代码未将输出写入文件。它只转储.data文件中的数据,而不是输出,范围从0到1。
import math
f = open('numeric.sm.data', 'r')
print f
maximum = 0
# get the maximum value
for input in f:
maximum = max(int(input), maximum)
f.close()
print('maximum ' + str(maximum))
# re-open the file to read in the values
f = open('numeric.sm.data', 'r')
print f
o = open('numeric_sm_results.txt', 'w')
print o
for input in f:
# method 1: Divide each value by max value
m1 = float(input) / (maximum)
o.write(input)
print repr(input.strip('\n')).rjust(5), repr(m1).rjust(5)
o.close()
f.close()
答案 0 :(得分:1)
o.write(input)
应该是
o.write(str(m1))
并且您可能想要添加换行符或其他内容:
o.write('{0}\n'.format(m1))
答案 1 :(得分:0)
这是因为你有一个名为f
的文件处理程序
但它只指向一个对象,而不是文件的内容
所以,
f = open('numeric.sm.data', 'r')
f = f.readlines()
f.close()and then,
然后,
o = open('numeric_sm_results.txt', 'w')
for input in f:
# method 1: Divide each value by max value
m1 = float(input) / (maximum)
o.write(input) # Use casting if needed, This is File write
print repr(input.strip('\n')).rjust(5), repr(m1).rjust(5) # This is console write
o.close()