0

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

10
  • 1
    I don't get this point: "I want that the code check if the holle line exist in the file: if "Companyname" = "company Name 4" it should exist. But if "Companyname" = "company Name 4" it must be wrong." Commented Oct 13, 2022 at 8:51
  • It had to match exactly as in the string. If string is only "Name 4" then it should "string does not exist". Commented Oct 13, 2022 at 8:58
  • It's unclear what the problem with your code is. What is the output and the error? Commented Oct 13, 2022 at 9:03
  • Buran, their is no error. I got an e-mail and the pyton script get the company name from the body of the content. And put it in a string called: Companyname (This is the first part of the code). Second part of the code is to check if the company name is in the file. But their come the issue. The out put is always: print('string does not exist'). even if your name was correct in the email. Commented Oct 13, 2022 at 9:13
  • I come again: please focus on what you write in this phrase: "I want that the code check if the holle line exist in the file: if "Companyname" = "company Name 4" it should exist. But if "Companyname" = "company Name 4" it must be wrong."; you say first that if this "Companyname" = "company Name 4" is in the text then is ok but later on you sat that the same string "Companyname" = "company Name 4" exist is worng Commented Oct 13, 2022 at 9:26

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

5 Comments

would it be helpful if he uses readline() instead read()..? Asking for learning not for curiosity
Only if you search for text on a specific line.
bn_In thank your for your help. however I don't know how to handle this in my code. Should I do this: path = ('Companyname.txt', 'r') and text = Companyname?
path is the string of the file's location. text is the string of what you're searching for. So in_file('c:\\some\\folder\\Companyname.txt', 'Company4') would look for Company4. With the open() function I omitted 'r' for read-only, as it's the default value. Hope this helps
@xlmaster one situation where readlines() would be much better would be if the file is large. As the file is read with only one line in memory at a time, rather than using read() which reads the whole file.
0

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 BreeBronne\r' in names
# Output
> False

print('Vakantiepark BreeBronne\r')
# Output
'Vakantiepark BreeBronne'

I would suggest to try:

if Bedrijfsnaam.strip() in file22:
   print('string exist')
else:
   print('string does not exist')

3 Comments

I can't upvote your Answers. but i works! Als i figure out that Bedrijfsnaam isn't an str but Any. @flow Thank you!!
But if Bedrijfsnaam contains 'Vakantiepark Bree' it give as output: true. But i would like to see the match exactly.
'Vakantiepark Bree' in names results in False if names is a list of strings. Only if it is not separated but a single string containing all your names it results in True. Did your splitting not succeed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.