我將一些新位添加到文本文件中的一行中,然後將其與文件中其餘行一起寫入新文件。參照第二,如果在while循環語句,我想這是所有在同一行:爲什麼寫這篇文章的一部分到了新的一行? (Python)
path = raw_input("Enter the name of the destination folder: ")
source_file = open("parameters")
lnum=1
for line in source_file:
nums = line.split()
if (lnum==10):
mTot = float(nums[0])
if (lnum==11):
qinit = float(nums[0])
if (lnum==12):
qfinal = float(nums[0])
if (lnum==13):
qgrowth = float(nums[0])
if (lnum==14):
K = float(nums[0])
lnum = lnum+1
q = qinit
m1 = mTot/(1+qinit)
m2 = (mTot*qinit)/(1+qinit)
taua = (1/3.7)*(mTot**(-4.0/3.0))
taue = taua/K
i = 1
infname = 'parameters'
while (q <= qfinal):
outfname = path+'/'+str(i)
oldfile = open(infname)
lnum=1
for line in oldfile:
if (lnum==17):
line = "{0:.2e}".format(m1)+' '+line
if (lnum==18):
line = "{0:.2e}".format(m2)+' '+line+' '+"{0:.2e}".format(taua)+' '+" {0:.2e}".format(taue)
newfile = open(outfname,'a')
newfile.write(line)
lnum=lnum+1
oldfile.close()
newfile.close()
i=i+1
q = q + q*(qgrowth)
m1 = mTot/(1+q)
m2 = (mTot*q)/(1+q)
但TAUA和taue正在於它的其餘部分線下寫的。我在這裏錯過了什麼?
請發佈沒有微不足道的語法錯誤的*實際*代碼。這不是有效的Python代碼;用必要的更正更新帖子。 (這也降低了發佈代碼和實際代碼在其他重要方面不同的機會。) – 2012-09-03 03:33:24