Python: Check if a specific string of text is in a file
Question:
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 name2n"
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 name2n" 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: 3574r', 'Ticket onderwerp: Storing slagboomr', 'Bedrijfsnaam: Vakantiepark BreeBronner', 'Naam: Dave B.r', 'Email: [email protected] <mailto:[email protected]> r', 'Telefoonnummer: 01234567891r', 'r', 'r', 'Bericht: Message from the user.r', 'UIT werkt wel.r', 'r', 'r', '*PS, dit is voor Julian Bot Hans:r', 'Bedrijfsnaam: Vakantiepark BreeBronner', 'Servicecontract: Basis Plus Contractr', '']
line3 = lines[16]
print(line3)
Value line3 = Bedrijfsnaam: Vakantiepark BreeBronne
Answers:
This function will return whether or not a string is contained in a text file.
def in_file(path, text):
with open(path) as f:
return text in f.read()
Following up on your comment:
print(Bedrijfsnaam)
file2 = open('Basis_Plus_Contract.txt')
content2 = file2.read()
file22 = content2.split("n") #Spareate the body contest in lines
print(file22)
if Bedrijfsnaam in file22:
print('string exist')
else:
print('string does not exist')
# Result:
# Print Result 1: Vakantiepark BreeBronne
# Print Result 2: ['Gemeente Zwolle', 'Gem Zwolle', 'Vakantiepark L', 'Vakantiepark BreeBronne', 'Vakantiepark BreeBronne', '']
# Print Result 3: string does not exist
Result 1 and 2 suggest that your code should print ‘string exist’ but it doesn’t. Whitespaces can explain that behaviour:
names = ['Gemeente Zwolle', 'Gem Zwolle', 'Vakantiepark L', 'Vakantiepark BreeBronne', 'Vakantiepark BreeBronne', '']
'Vakantiepark BreeBronne' in names
# Output
> True
'Vakantiepark BreeBronner' in names
# Output
> False
print('Vakantiepark BreeBronner')
# Output
'Vakantiepark BreeBronne'
I would suggest to try:
if Bedrijfsnaam.strip() in file22:
print('string exist')
else:
print('string does not exist')
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 name2n"
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 name2n" 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: 3574r', 'Ticket onderwerp: Storing slagboomr', 'Bedrijfsnaam: Vakantiepark BreeBronner', 'Naam: Dave B.r', 'Email: [email protected] <mailto:[email protected]> r', 'Telefoonnummer: 01234567891r', 'r', 'r', 'Bericht: Message from the user.r', 'UIT werkt wel.r', 'r', 'r', '*PS, dit is voor Julian Bot Hans:r', 'Bedrijfsnaam: Vakantiepark BreeBronner', 'Servicecontract: Basis Plus Contractr', '']
line3 = lines[16]
print(line3)
Value line3 = Bedrijfsnaam: Vakantiepark BreeBronne
This function will return whether or not a string is contained in a text file.
def in_file(path, text):
with open(path) as f:
return text in f.read()
Following up on your comment:
print(Bedrijfsnaam)
file2 = open('Basis_Plus_Contract.txt')
content2 = file2.read()
file22 = content2.split("n") #Spareate the body contest in lines
print(file22)
if Bedrijfsnaam in file22:
print('string exist')
else:
print('string does not exist')
# Result:
# Print Result 1: Vakantiepark BreeBronne
# Print Result 2: ['Gemeente Zwolle', 'Gem Zwolle', 'Vakantiepark L', 'Vakantiepark BreeBronne', 'Vakantiepark BreeBronne', '']
# Print Result 3: string does not exist
Result 1 and 2 suggest that your code should print ‘string exist’ but it doesn’t. Whitespaces can explain that behaviour:
names = ['Gemeente Zwolle', 'Gem Zwolle', 'Vakantiepark L', 'Vakantiepark BreeBronne', 'Vakantiepark BreeBronne', '']
'Vakantiepark BreeBronne' in names
# Output
> True
'Vakantiepark BreeBronner' in names
# Output
> False
print('Vakantiepark BreeBronner')
# Output
'Vakantiepark BreeBronne'
I would suggest to try:
if Bedrijfsnaam.strip() in file22:
print('string exist')
else:
print('string does not exist')