Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My input string from a file is
ANG
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
My required output should be
ANG
0.0
0.0
0.0
0.0
0.0
0.0
100
0.3
But Now my input is same as output .The last two zeroes under ANG string to be replaced as 100.0 and 0.3 respectively.Please help me to get the required output.

What I have tried:

Python
import re
class Calculate:
    def angCal(self,direction,magnitude,string):
        cnt=0
        if cnt<=6:
            f4.write(string)
            cnt+=1
        elif cnt==7:
            string=str(direction)+"\n"
            f4.write(string)
            cnt += 1
        elif cnt==8:
            string = str(magnitude) + "\n"
            f4.write(string)
            cnt += 1
        elif cnt==9:
            cnt += 1
angFlag=0
angcnt=0
f1=open("nominal.txt","r")
f4=open("pp.txt","w")
obj=Calculate()
for line in f1.readlines():
    string=line
    if re.match(r'ANG',string):
        angFlag=1
        f4.write(string)
    elif angFlag==1 and angcnt==0:

        direction=100
        magnitude=0.3
        print(string)
        obj.angCal(direction,magnitude,string)
        angFlag=0
        angcnt+=1
    elif angFlag==1 and angcnt==1:
        direction=315
        magnitude=0.4
    else:
        f4.write(string)
Posted
Updated 28-Oct-18 6:17am
v2
Comments
Mohibur Rashid 28-Oct-18 17:30pm    
I understand your requirement but I don't understand your reason for this. It's required for sanity!
Mohibur Rashid 28-Oct-18 17:31pm    
Instead if looping, add all in array and replace last 2
BinshaS 29-Oct-18 0:11am    
How to add these in array .Please help
BinshaS 29-Oct-18 0:11am    
Can you help me with a code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900