0

I am using text that is stored from the file.txt and it used for some analyses. After reading text from that file i seeing difference in printed variable value and actual value stored in that variable

file=open("message.txt", "r")
message=file.read()
print(message)

Printed output: 'MSH|^~\\&|GHH LAB|ELAB-3|GHH OE|BLDG4|200202150930||ORU^R01|CNTRL-3456|P|2.4'

Above result contains printed value of my message. Now i am comparing printed value with the actual value in the message

if message == 'MSH|^~\\&|GHH LAB|ELAB-3|GHH OE|BLDG4|200202150930||ORU^R01|CNTRL-3456|P|2.4':
        print("match found")
else:
        print("No match found")

Output: No Match found

I tried with str() and repr() methods again it is not returning the correct result

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

0

You need to escape the backslash characters in the string literal in your code using \\\\. Also you should strip() the contents of the file as mentioned.

The following works for me:

file = open("message.txt", "r")
message = file.read().strip()
print(message)
if message == 'MSH|^~\\\\&|GHH LAB|ELAB-3|GHH OE|BLDG4|200202150930||ORU^R01|CNTRL-3456|P|2.4':
        print("match found")
else:
        print("No match found")
Holy Mackerel
  • 3,259
  • 1
  • 25
  • 41