I want to have a Python program that will read through a text file, then print whether or not a specific string was found in that file.
Here is the code that I can't get working:
#Company name ----- -----
line3 = lines[16]
line3split = line3.split(":")
line3split2 = line3split[1].split(' ', 1)
Companyname = line3split2 [1]
print(Companyname) #To check what is the output
print(type(Companyname)) #To check what is the type <class 'str'>
with open('Companyname.txt', 'r') as file:
content = file.read()
if Companyname in content:
print('string exist')
else:
print('string does not exist')
Some content: lines[16] comes from a message from an outlook body content. I split the content of the body in lines and on line 16 their is the line i need for the check.
Companyname.txt look likes:
Company Name1
Company name2
Company Name 3
company Name4
I want that the code check if the holle line exist in the file: if "Companyname" = "company Name4" it should exist. But if "Companyname" = "company Name 4" it must be wrong.
When i use this code (It wil work):
with open('Companyname.txt', 'r') as file:
content = file.read()
Companyname2 = "Company name2\n"
if Companyname2 in content:
print('string exist')
else:
print('string does not exist')
Or
with open('Companyname.txt', 'r') as file:
content = file.read()
if "Company name2\n" in content:
print('string exist')
else:
print('string does not exist')
But it have to come from string "Companyname".
On request from "Lucas M. Uriarte"
BodyMessage = message.body #But it in string
lines = BodyMessage.split("\n") #Spareate the body contest in lines
print(lines)
Value lines = ['Nieuwe Servicedesk ticket:\r', '\r', 'Ticketnummer: 3574\r', 'Ticket onderwerp: Storing slagboom\r', 'Bedrijfsnaam: Vakantiepark BreeBronne\r', 'Naam: Dave B.\r', 'Email: [email protected] <mailto:[email protected]> \r', 'Telefoonnummer: 01234567891\r', '\r', '\r', 'Bericht: Message from the user.\r', 'UIT werkt wel.\r', '\r', '\r', '*PS, dit is voor Julian Bot Hans:\r', 'Bedrijfsnaam: Vakantiepark BreeBronne\r', 'Servicecontract: Basis Plus Contract\r', '']
line3 = lines[16]
print(line3)
Value line3 = Bedrijfsnaam: Vakantiepark BreeBronne