Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

python - change the value after passing each line in the txt file by the value n

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I use p variable to get output that you want. You can modify your code like that.

import os
import sys
import numpy as np
import re

f = open("demofile.txt", "r")
lines = f.readlines()
#sys.stdout = open("results.txt", "w")
p = 1
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))*p
        print(left)
        p +=1

Also I changed your code. Maybe it works like that,

n=10
for lines in file:
    if lines[-1] == '
':
       left=c1+n*np.ones((1))*((1,2,3))
       n=n+10

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...