繁体   English   中英

有没有办法使用用户在 python 中输入的答案? (使用查询器)

[英]Is there a way to use the answers entered by the user in python? (using inquirer)

python 初学者在这里。 我正在尝试创建一个脚本,该脚本根据设置的规则(没有空格、数字、没有特殊字符......)递归地重命名文件夹中的文件。 为此,我通过在列表中选择(全部使用查询器)来询问用户他们想要如何重命名他们的文件。 问题是我不能将用户的答案与询问者一起使用。 这是代码:

import os
import re
import inquirer

def space():
    for file in os.listdir("."):
        filewspace = file.replace(" ","")
        if(filewspace != file):
            os.rename(file,filewspace)

def numbers():
    for file in os.listdir("."):
        os.rename(file, file.translate(str.maketrans('','','0123456789')))

questions = [
  inquirer.List('choices',
                message="How do you want to format?",
                choices=['Remove spaces', 'Remove numbers',
                'Remove specials',],
            ),
]
answers = inquirer.prompt(questions)

if (answers) == "space":
    space()
elif (answers) == "numbers":
    numbers()
#else:
    #specials()

当我尝试在用户选择后打印“答案”中的内容时,我得到了回复: https://i.stack.imgur.com/UrraB.jpg

我被困住了......有人可以帮忙吗?

似乎变量answers包含一个字典。 因此, if检查底部的内容,您必须更改您:

if answers["choices"] == "Remove spaces":
    space()
elif answers["choices"] == "Remove numbers":
    numbers()
...

那应该具有所需的功能。

应该使用字典。

if (answers['choices'] == "Remove spaces"):
    space()
elif (answers['choices'] == "Remove numbers"):
    numbers()
#else:
    #specials()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM