keyword checker - keyword.kwlist

Discussion in 'Python' started by tom, May 10, 2007.

  1. tom

    tom Guest

    Hi

    I try to check whether a given input is keyword or not. However this
    script won't identify keyword input as a keyword. How should I modify it
    to make it work?

    #!usr/bin/env python
    import keyword

    input = raw_input('Enter identifier to check >> ')
    if input in keyword.kwlist:
    print input + "is keyword"

    else:
    print input + "is not keyword"
     
    tom, May 10, 2007
    #1
    1. Advertisements

  2. Hmm... I tried, and identify it.
    Try to change the 'input' variable name with other...
     
    alessiogiovanni.baroni, May 10, 2007
    #2
    1. Advertisements

  3. tom

    tom Guest

    Changed input variable to myInput, but the result is still the same.

    for example, 'else' isn't identified as a keyword by the script though
    it exists in keyword.kwlist.
     
    tom, May 10, 2007
    #3
  4. Changed input variable to myInput, but the result is still the same.[/QUOTE]

    That was good advice, but isn't going to help here. Because "input"
    isn't a keyword, it's a builtin. If you want to check builtins as
    well as keywords, you need(Although obviously you'd pre-build that list.)
    .... input = raw_input('Enter identifier to check >> ')
    .... if input in keyword.kwlist:
    .... print input, "is keyword"
    .... else:
    .... print input, "is not keyword"
    ....
    Enter identifier to check >> input
    input is not keywordEnter identifier to check >> else
    else is keyword
     
    Sion Arrowsmith, May 10, 2007
    #4
  5. It works for me.

    Try printing keyword.__file__ to make sure you are importing the right
    file. Also try printing keyword.kwlist.
     
    Steven D'Aprano, May 10, 2007
    #5
  6. tom

    tom Guest

    Still no go. I just can't get it right. My current script:

    #!usr/bin/env python
    import keyword

    myInput = raw_input('Enter identifier to check >> ')
    if myInput in keyword.kwlist:
    print myInput, "is keyword"

    else:
    print myInput, "is not keyword"

    print keyword.kwlist
    print keyword.__file__

    And the output:

    Enter identifier to check >> else
    else
    is not keyword
    ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del',
    'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global',
    'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print',
    'raise', 'return', 'try', 'while', 'with', 'yield']
    F:\Ohjelmat\Python25\Lib\keyword.pyc
     
    tom, May 10, 2007
    #6
  7. tom

    Peter Otten Guest

    Does

    myInput = raw_input("...").strip()
    # ...

    work?

    Your raw_input() seems to be broken and includes the trailing newline.

    Peter
     
    Peter Otten, May 10, 2007
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.