I'm trying to increase the value after each line traveled in the txt file by a certain amount
import os
import sys
import numpy as np
import re
f = open("demofile.txt", "r")
lines = f.readlines()
#sys.stdout = open("results.txt", "w")
for i in list(lines):
if i[0] != '<' and i[0] != '>' and i[0] != '=':
p = str(' '.join(i.split()))
print(p)
else:
w = i[3:]
frst=i[2]
w = ', '.join(w.split())
y = i[2]
y=int(y)+1
c=np.array([w])
c1 = [int(i) for i in c[0].replace(" ", "").split(",")
frst=str(frst).replace("[",'')
frst=str(frst).replace("]",'')
frst=int(frst)
c1=np.array(c1)
c1=np.array([c1]*frst)
c1=np.transpose(c1)
#this is the part of the code where I edit it
left=c1+10*np.ones((1))*((1,2,3))
print(left)
demofile.txt
<=3 1 2 3 4 #first iteration
<=3 1 2 3 4 #second iteration
Each line is one iteration. This is how every line goes through and I work with it. I try to increase each iteration by a certain amount
example
left=c1+10*np.ones((1))*((1,2,3)) #first iteration
left=c1+20*np.ones((1))*((1,2,3)) # second iteration
my output
1st iteration
[[11. 21. 31.]
[12. 22. 32.]
[13. 23. 33.]
[14. 24. 34.]]
2nd iteration
[[11. 21. 31.]
[12. 22. 32.]
[13. 23. 33.]
[14. 24. 34.]]
i need
1st iteration
[[11. 21. 31.]
[12. 22. 32.]
[13. 23. 33.]
[14. 24. 34.]]
2nd iteration
[[21. 41. 61.]
[22. 42. 62.]
[23. 43. 63.]
[24. 44. 64.]]...etc
I tried to find the end of the line but I couldn't incorporate it into the code as needed
for lines in file:
n=10
if lines[-1] == '
':
left=c1+n*np.ones((1))*((1,2,3))
n=n+10
question from:
https://stackoverflow.com/questions/65939241/change-the-value-after-passing-each-line-in-the-txt-file-by-the-value-n 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…