python - 如何仅验证扩展名为 .xlsx 的文件名?

标签 python excel validation user-interface types

我有一个基于 Gooey 的 GUI,用户可以在其中输入要在程序末尾创建的输出文件的名称。但是,我希望只能添加最后带有 .xlsx 扩展名的文件名。

这是我的验证器:

 parser.add_argument('-Choose_File_Name',
                        action='store',
                        help="Output File Name with .xlsx",
                         gooey_options={
                             'validator': {
                                 'test': 'str(user_input) == .xlsx',
                                 'message': 'Must contain .xlsx at the end!'
                                 }
                             })

但是,它给了我“test”行无效的语法错误:“str(user_input) == .xlsx”

最佳答案

使用str.endswith

例如:

parser.add_argument('-Choose_File_Name',
                        action='store',
                        help="Output File Name with .xlsx",
                         gooey_options={
                             'validator': {
                                 'test': 'user_input.endswith(".xlsx") == True',
                                 'message': 'Must contain .xlsx at the end!'
                                 }
                             })

关于python - 如何仅验证扩展名为 .xlsx 的文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57305083/

相关文章:

validation - 如何使用 sfForm 验证没有日期的日期?

python - 在 Python 中查找将一个集群列表转换为另一个集群列表的映射

c# - 如何使用 C# 在 Excel 中为标签提供自定义角度

php - Yii2唯一验证错误:数组到字符串的转换

php - 使用 $.post() 进行表单验证

java - 尝试连接到 MS Excel 2007 [不创建 DSN] - 错误 : Data source name not found and no default driver specified

python - 使用 unicode 以任何语言保存文件

python - Django翻译创建po和mo文件,但翻译失败并显示翻译占位符

python - 将 Python 稀疏矩阵的行和列乘以数组中的元素

c# - 通过匹配特定单元格或数据(日期)将数据从一张纸复制/粘贴到另一张纸