[Python] When veryfiying if an element is inside a dict in python it doesn't work as expected

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a program in python which verifies if my clipboard starts with a specific thing, then it transforms the clipboard into something else, and it verifies if the clipboard now is inside a dictionary. Which, in some cases it is, (the key of an element in the dict) but it doesn't recognises it as so. Here is the code:

    def watcher() -> None:
    generators = {"first_name": faker.first_name()}
    while True:
    try:
    clipboard_input = pyperclip.paste()
    if clipboard_input.startswith("__gen__"):
    clipboard_output = str(clipboard_input.replace("__gen__", ""))
    print("clipboard_output:", clipboard_output)
    print("generators:", generators)
    print(clipboard_output in generators)
    if clipboard_output in generators:
    clipboard_output = generators[clipboard_output]
    typer.echo(
    f"Replace {clipboard_input} with {clipboard_output}",
    )
    pyperclip.copy(clipboard_output)
    sleep(1)
    except KeyboardInterrupt:
    break
    return None


    And here is the output. the command before is to show you my clipboard, then you have the output of this function:


    ^C(env) [nathanv@bongstong bonus_project0]$ echo __gen__first_name | xclip -selection clipboard
    (env) [nathanv@bongstong bonus_project0]$ python main.py gen watcher
    clipboard_output: first_name

    generators: {'first_name': 'Jacob'}
    False
    Replace __gen__first_name
    with first_name


    You can also see some debugging print statements. Any help will be much appreciated.

    Continue reading...

Compartilhe esta Página