Python Forum
Strip does not work when applied on a string read from a text file.
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strip does not work when applied on a string read from a text file.
#1
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:

#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()
Reply
#2
What is desired result?

  • Why create so many variables if you need only one?
  • Why iterate over all lines in file and retain only last row status (every iteration of for-loop overwrites value of check and you will have it only for last row)?.
  • What do you mean by "9 character string becomes 19 character due to added spaces when code reads it from the file"?

First word in line is 9 characters (except in line 9 where it's 10)

with open('data_log.txt', 'r', encoding='UTF-8') as f:
    for line in f:
        value = line.split()[0]
        print(len(value))
9
9
9
9
9
9
9
9
10
9
9
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Recommended way to read/create PDF file? Winfried 3 2,864 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,422 Nov-09-2023, 10:56 AM
Last Post: mg24
  Need to replace a string with a file (HTML file) tester_V 1 754 Aug-30-2023, 03:42 AM
Last Post: Larz60+
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,122 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  read file txt on my pc to telegram bot api Tupa 0 1,096 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,103 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,248 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,465 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,191 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,583 Feb-18-2023, 02:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020