Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
When I run this code:
str1= "Ram's birthplace is “Ayodhya” in Uttar Pradesh" file = open("abc.txt","w") file.write(str1)
I got error:
SyntaxError: Non-ASCII character '\xe2' in file, no encoding declared
file = open("abc.txt","w",encoding="utf-8")
Looks like you need to set the encoding on the top of your script to prevent this error.
Ex:
# -*- coding: utf-8 -*-
Add a comment
As Rakesh pointed out, you can set an encoding cookie at the top of your script so that it handles non-ascii characters.
Alternatively, you could replace the double quotation marks around Ayodhya with ascii quotation marks:
str1= "Ram's birthplace is \"Ayodhya\" in Uttar Pradesh"
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
file = open("abc.txt","w",encoding="utf-8"), or maybe it's decoding