Question
I am trying to write a code that will access a .txt file and take values from a list in that .txt file and assign
I am trying to write a code that will access a .txt file and take values from a list in that .txt file and assign it to variables in my code.
I initially set the variables I want to assign the values with, with the name of the "title" for each value, and I loop through the line which the "title" is present and record the index of the character which my value starts and then I assign that index as the start until the end of the line - for capturing the whole value.
My code works fine for all the variable/value, BUT ONE!!!!! Can somebody help to understand what is wrong, and why the loop repeats itself for that specific line ( it should be iterations over a string of characters so I don't see why this would possibly happen in consequence of the number I have in the index of = +2) The problematic block is regarding P602 and it is marked in bold bellow.
Please help... I don't know why it works for all other blocks and has such erratic behavior for this specific P602 block (ps: if I change value in the .txt file from 0 to 0.0 or from 0 to 3000 it works - so it is not even regarding being float or integer as certain integers work fine, and others also show the loop happens more than once giving the wrong desired output.
Thank you for your help in advance.
This is my code:
from pathlib import Path parms_data = Path('C:\\Users\\Jbuckle\\PycharmProjects\\pythonProject\\parms.dat').read_text() P102 = "x_parms(1)" P103 = "x_parms(2)" P109 = "x_parms(8)" P115 = "x_parms(14)" P118 = "x_parms(17)" P201 = "y_parms(0)" P202 = "y_parms(1)" P203 = "y_parms(2)" P209 = "y_parms(8)" P215 = "y_parms(14)" P218 = "y_parms(17)" P501 = "a_parms(0)" P602 = "c_parms(1)" T1Rad = "n_parms(0)" T2Rad = "n_parms(1)" T3Rad = "n_parms(14)" T4Rad = "n_parms(17)" TdRad = "n_parms(2)" P141 = "x_parms(40)" P241 = "y_parms(40)" P741 = "n_parms(40)" with open('parms.dat', 'r') as file: while 1: lines = file.readlines() if not lines: break for line in lines: if P102 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P102 = value print(P102) for line in lines: if P103 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P103 = value print(P103) for line in lines: if P109 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P109 = value print(P109) for line in lines: if P115 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P115 = value print(P115) for line in lines: if P118 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P118 = value print(P118) for line in lines: if P201 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P201 = value print(P201) for line in lines: if P202 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P202 = value print(P202) for line in lines: if P203 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P203 = value print(P203) for line in lines: if P209 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P209 = value print(P209) for line in lines: if P215 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P215 = value print(P215) for line in lines: if P218 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P218 = value print(P218) for line in lines: if P501 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P501 = value print(P501) for line in lines: if P602 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:-1] P602 = value #print("one more loop") print(P602) for line in lines: if T1Rad in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] T1Rad = value print(T1Rad) for line in lines: if T2Rad in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] T2Rad = value print(T2Rad) for line in lines: if T3Rad in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] T3Rad = value print(T3Rad) for line in lines: if T4Rad in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] T4Rad = value print(T4Rad) for line in lines: if TdRad in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] TdRad = value print(TdRad) for line in lines: if P141 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P141 = value print(P141) for line in lines: if P241 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P241 = value print(P241) for line in lines: if P741 in line: for ch in line: if ch == '=': pos_value = line.index(ch)+2 value = line[pos_value:] P741 = value print(P741) !!!!!!!!I will try and post the content of file as a comment on this question as doesn't let me exceed 16000 characters in this post!!!!!!!!!!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started