Python non-ascii character character error
I am a beginner programmer trying to write a python script that will generate random passwords. However, I always get a non-ASCII error even though I declared the encoding # utf-8 as mentioned in another similar question here on Stack Overflow. This is the source code:
import string
import random
#coding: utf-8
print "Password generator will create a random customizable password."
print "Choose your options wisely."
number = int(input("How many letters do you want in your password?"))
caps = str(input("Do you want capital letters in your password? Y/N"))
symbols = str(input( "Do you want punctuation, numbers and other symbols in your password? Y/N"))
punctuation = ("!", ".", ":", ";", ",", "?", "'", "@", "£", "$", "«", "»", "~", "^","%", "#", "&", "/", range(0, 11))
lowercase = string.ascii_lowercase
uppercase = string.ascii_uppercase
if caps == "N":
characters = lowercase
else:
characters = uppercase + lowercase
if symbols == "Y":
characters += punctuation
password = random.sample(characters, number)
print "The password is", password "."
This is the terminal output
pedro @ pedro-Inspiron-3521: ~ / Desktop $ python passwordgenerator.py
File "passwordgenerator.py" line 9 SyntaxError: non-ASCII character '\ xc2' in passwordgenerator.py on line 9, but no encoding is declared; See http://www.python.org/peps/pep-0263.html for details
I checked the link but I couldn't figure it out, maybe because I am not a native English speaker and since I recently started programming. Thank you for your help.
source to share
Do you have symbols >>
and <<
punctuation list.
Invalid. Try to find their unicode code instead
http://www.fileformat.info/info/unicode/char/bb/index.htm http://www.fileformat.info/info/unicode/char/ab/index.htm
EDIT: Oh, but wait, this is a password generator, so NO doesn't even put double chevrons as part of the punctuation list, as they are invalid in any password.
punctuation = ("!", ".", ":", ";", ",", "?", "'", "@", "$", "~", "^", "%", "#", "&", "/", range(0, 11))
Also, why are you adding numbers to the punctuation tuple?
Why not use string.punctuation and string.digits instead?
source to share
The line #coding: utf-8
must be at the top of the file to allow non-ASCII characters.
£
, «
and »
are not ASCII.
While you probably don't need non-ascii characters in your password, and they won't be allowed on most sites, there is no reason why they cannot theoretically be allowed.
source to share
If you are using a Mac, there is a .DS_STORE file that throws an ascii error when reading the contents of your folder.
So if you are using mac
-
Delete all DS_Store files
-
Select Applications> Utilities to launch a terminal.
-
Enter the following UNIX command:
-
sudo find / -name ".DS_Store" -depth -exec rm {} \;
-
When prompted for a password, enter your Mac OS X Administrator password
source to share