Hello everyone,
I am trying to read a file and then put the data at appropriate location.
Here is the code,
import time
import threading
import os
import glob
def ReadFile():
contents = "C:\\Users\\kshah2\\Desktop\\MDR1.asc"
if system.file.fileExists(contents):
File = system.file.readFileAsString(contents)
line_num2 = 0
Read = open(contents, "r")
for line in Read.readlines():
line_num2 += 1
if line.find("DATAPOINT") >= 0:
CheckData(line_num2 - 1)
elif event.source.value == 19:
system.gui.messageBox("No File Exists")
def CheckData(line_num2):
contents = open("C:\\Users\\kshah2\\Desktop\\MDR1.asc", "r")
Result = {}
caption = []
values = []
Curve1data = []
pointer = 0
curves = 0
curve_number = 0
curve_pointer = 0
for line in contents.readlines():
if pointer == 0 and line.find("DATAPOINT") >= 0:
pointer = 1
elif pointer == 1 and line.find("Variable.Caption") >= 0:
pointer = 2
C_data = line.split("=")
C_data = C_data[1].strip()
caption.append(str(C_data))
elif pointer == 2 and line.find("Variable.Value") >= 0:
pointer = 0
C_data = line.split("=")
C_data = C_data[1].strip()
values.append(str(C_data))
elif pointer == 0 and line.find("[CURVE") >= 0 :
curves = 1
curve_number += 1
curve_pointer = 0
elif curves == 1:
if curve_number == 1:
if line.find("Variable.Caption") >= 0 and curve_pointer == 0:
curve_pointer = 1
C_data = line.split("=")
C_data = C_data[1].strip()
Curve1name = str(C_data)
elif curve_pointer == 2 and line.find("D"):
C_data = line.split("=")
C_data = C_data[1].strip()
Curve1data.append(str(C_data))
elif line.find("D0") >= 0 and curve_pointer == 1:
curve_pointer = 2
C_data = line.split("=")
C_data = C_data[1].strip()
Curve1data.append(str(C_data))
print C_data
Result = dict(zip(caption, values))
print Result
print Curve1data
print Curve1name
ReadFile()
For the captions and values, I am getting expected results, but, I am getting index out of range error for ,
elif line.find("D0") >= 0 and curve_pointer == 1:
curve_pointer = 2
C_data = line.split("=")
C_data = C_data[1].strip()
Curve1data.append(str(C_data))
I think the problem is it remembers the value of C_data from previous line and then somehow doesn’t overwrite it.
Please let me know if you understand the problem and have some solution.
Help is always appreciated.