4

I'm a beginner programmer trying to write a python script that will generate random passwords. However, I always get a non-ASCII character error even though I declared the coding #utf-8, as mentioned in another similar question here in 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 file passwordgenerator.py on line 9, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

I checked the link but I couldn't understand a thing, maybe because I'm not a native english speaker and since I started programming not long ago.Thanks in advance for your help.

1
  • use raw_input() on Python 2.7 instead of input(). The latter evals its input i.e., it works like eval(raw_input()). Commented Oct 11, 2014 at 19:36

3 Answers 3

5

The #coding: utf-8 line needs to be at the top of the file to allow non-ASCII characters.

£, «, and », are not ASCII.

While you probably don't want non-ascii characters in your password, and they wouldn't be allowed on most sites, there is no reason they couldn't theoretically be permitted.

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

Comments

2

You have the >> and << symbols in your punctuations list.

These are not valid. 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 do not even put the double chevrons as part of the punctuations list, as these are not valid in any password.

punctuation = ("!", ".", ":", ";", ",", "?", "'", "@", "$", "~", "^", "%", "#", "&", "/", range(0, 11))

Also, why do you add digits to the punctuation tuple?

Why not using string.punctuation and string.digits instead?

14 Comments

So in the links you gave me it says that « in python source code is u"\u00AB"and » is u"\u00BB". Should I include this inside "" or not?
I deleted them, but it's still not working. I get the same mistake
Here is the punctuation variable that I currently have: punctuation = ("!", ".", ":", ";", ",", "?", "'", "@", "£", "$", "~", "^","%", "#", "&", "/", range(0, 11))
To answer your question, I have no idea what a digit is, or what string.punctuation and string.digits would do. However, I'll surely search it. Thanks, kind stranger
Sorry @agf and Apero , can't upvote any of your answers, tried to and it says Vote up requires 15 reputation :(
|
0

If your are using Mac, there is file .DS_STORE which causes the ascii error while reading contents of your folder.

So do these if you're using mac

  • Remove all DS_Store files

  • Select Applications > Utilities to launch 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

1 Comment

While this is a good answer and possible, a more likely cause on a Mac is copying from an editor to a terminal which leads to non-Python friendly leading whitespace. Simply replacing the leading whitespace will correct the issue.

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.