我有一个非常基本的问题。我编写了一个代码,打开一个包含数字的.txt文件,1 2 3 4 5 6 7 8 9
然后将其平方并写入其他文件。现在,我想添加到此代码过程中,该过程将所有这些数字分成几行并重写,如下所示:
1 4 9
16 25 36
49 64 81
我的代码已经:
n=[]
dane = open("num.txt", "r")
for i in dane:
i = i.replace('\n','')
for j in i.split(' '):
j = int(j)
j = j**2
n.append(j)
nowy = open("newnum.txt","w")
nowy.write(str(n))
nowy.close()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句