Hi,
I am trying to do this:
1. Read string line of data from a log file one line at a time.
2. Split string by identifying '\t' and assign then to variable.
3. Compare one of the string variable value with self-defined string value.
4. if this comparison shall be true then execute a conditional logic.
I am stuck at step 3 described above. The value does not match no matter what due to the fact that a 9 character string becomes 19 character due to added spaces when code reads it from the file.
I have tried performing strip, replace and even adapting self-defined string by adding additional spaces but comparison still holds false.
Data-log file has below data:
Data_Val2 12/25/2018 15:15 0 1 43459635444
Data_Val3 12/25/2018 15:15 0 1 43459635444
Data_Val4 12/25/2018 15:15 0 1 43459635444
Data_Val5 12/25/2018 15:15 0 1 43459635444
Data_Val6 12/25/2018 15:15 0 1 43459635444
Data_Val7 12/25/2018 15:15 0 1 43459635444
Data_Val8 12/25/2018 15:15 0 1 43459635444
Data_Val9 12/25/2018 15:15 0 1 43459635444
Data_Val10 12/25/2018 15:15 0 1 43459635444
Data_Val1 12/25/2018 15:15 0 1 43459635444
Data_Val2 12/25/2018 15:15 361 1 43459635456
Python code is below:
I am trying to do this:
1. Read string line of data from a log file one line at a time.
2. Split string by identifying '\t' and assign then to variable.
3. Compare one of the string variable value with self-defined string value.
4. if this comparison shall be true then execute a conditional logic.
I am stuck at step 3 described above. The value does not match no matter what due to the fact that a 9 character string becomes 19 character due to added spaces when code reads it from the file.
I have tried performing strip, replace and even adapting self-defined string by adding additional spaces but comparison still holds false.
Data-log file has below data:
Data_Val2 12/25/2018 15:15 0 1 43459635444
Data_Val3 12/25/2018 15:15 0 1 43459635444
Data_Val4 12/25/2018 15:15 0 1 43459635444
Data_Val5 12/25/2018 15:15 0 1 43459635444
Data_Val6 12/25/2018 15:15 0 1 43459635444
Data_Val7 12/25/2018 15:15 0 1 43459635444
Data_Val8 12/25/2018 15:15 0 1 43459635444
Data_Val9 12/25/2018 15:15 0 1 43459635444
Data_Val10 12/25/2018 15:15 0 1 43459635444
Data_Val1 12/25/2018 15:15 0 1 43459635444
Data_Val2 12/25/2018 15:15 361 1 43459635456
Python code is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#open file dataFile = open ( 'DataFile_0.txt' ) #define file path to be opened for dataStr in dataFile: strLen = len (dataStr) # find length of characters from the line being read. if strLen > 2 : # execute code only if line character length is more than 2 to skip executing empty lines (varName, dateTime, varValue, validity, timeInMs) = dataStr.split(sep = '\t' , maxsplit = 6 ) # each data line contains 5 type of data seperated by tab character dataList = [varName, dateTime, varValue, validity, timeInMs] # creating list of data splatted var_Data3_a = " D a t a _ V a l 3 " var_Data3_a1 = var_Data3_a.replace( " " , " " ) # try to increse the string to 19 characters to match it with "var_Data3_b1" value var_Data3_b = str (dataList[ 0 ]) var_Data3_b1 = var_Data3_b.replace( " " , "_" ) # this line is not able to replace " " with "_" var_Data3_a1_len = len (var_Data3_a1) #measure the character lengths of a1 data var_Data3_b1_len = len (var_Data3_b1) #measure the character lengths of b1 data if var_Data3_a1 = = var_Data3_b1: # checking if string in data read from file & data defined in program has become same or not. check = "true" else : check = "false" continue #Jump to next iteration of for loop if strLen<=2 #close file dataFile.close() |