0

So i have this file called test

But I must explain to you how all this mistaken idea of denounc ing pleasure and praising pain was born and I will give you a complet e account of the system, and expound the actual teachings of the great explo rer of the truth, the master-builder of human happiness. No one rejects, disli kes, or avoids pleasure itself, because it is pleasure, but because those who do no t know how to pursue pleasure rationally encounter consequences that are extremely painful . Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to ob tain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure tha t has no annoying consequences, or one who avoids a pain that produces no resultant pleasure

And this code in python

with open('test.txt', 'r') as f:
    for line in f:
        line=line.strip()
        if 'trivial' in line:
            print(line)

However, it doesn't print anything, but if i replace 'trivial' for a letter like 't', i'tll print any lines with a 't' on it. What am i doing wrong?

8
  • 3
    It works fine for me and prints the line with 'trivial' in it Commented May 15, 2017 at 11:12
  • That should definitely work, the only possibility is that "trivial" is not in the file. Commented May 15, 2017 at 11:16
  • Im using windows prompt to execute it with python, can that be the problem? Commented May 15, 2017 at 11:17
  • I guess your file location and the path from which you have opened your prompt might have been different Commented May 15, 2017 at 11:19
  • Try giving the exact path to your file when you read it Commented May 15, 2017 at 11:20

1 Answer 1

1

Ok i got it. Just needed to add enconding utf 16

with open("test.txt", "r", encoding="utf-16") as f:
    for line in f:
        if 'trivial' in line:
            print(line)

feels bad man

Sign up to request clarification or add additional context in comments.

Comments

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.