简体   繁体   English

我在 python 中尝试了 case 语句,但每次我运行时我都会得到我的默认值

[英]i tried case statement in python but every time i run i got my default as a result

Exten=input("Filename:")
match Exten:
    case "Gif":
        print("image/gif")
    case "jeg":
        print("image/jpeg")
    case "jpeg":
        print("image/jpeg")
    case "png":
        print("image/png")
    case "pdf":
        print("application/pdf")
    case "txt":
        print("text/plain")
    case "zip":
        print("application/zip")
    case default:
        print("application/octet-stream")

whatever file name I got only output is default, please explain where did I miss无论我得到什么文件名 output 是默认的,请解释我错过了哪里

Maybe you should split to get the extension.也许您应该拆分以获取扩展名。 I think you get the whole name of the file and it does not match.我认为你得到了文件的全名,但它不匹配。

fileName=input("Filename with extension:") 
Exten = data.split(".")    
match Exten: 
    case ‘Gif‘: print("image/gif") 
    case ‘jeg‘: print("image/jpeg") 
    case ‘jpeg‘: print("image/jpeg") 
    case ‘png‘: print("image/png") 
    case ‘pdf‘: print("application/pdf") 
    case ‘txt‘: print("text/plain") 
    case ‘zip‘: print("application/zip") 
    case default: print("application/octet-stream")

If Exten is a filename, you need to match on the suffix, not the entire filename.如果 Exten 是文件名,则需要匹配后缀,而不是整个文件名。 You can use split() for this.您可以为此使用 split() 。 Split the string at the period which will create a list with 2 items, the filename and the suffix.在将创建包含 2 个项目、文件名和后缀的列表的句点处拆分字符串。 You just need to add a line to split and change the match to only use the second item of the list.您只需要添加一行来拆分并将匹配更改为仅使用列表的第二项。

Exten=input("Filename:")
exten_split = Exten.split(".")
match exten_split[1]:

You can find examples of this at https://www.w3schools.com/python/ref_string_split.asp您可以在https://www.w3schools.com/python/ref_string_split.asp找到这方面的示例

暂无
暂无

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

相关问题 为什么每次运行循环时都没有得到相同的结果? - Why am I not getting the same result every time I run through my loop? 无法解决这个python问题我每次都出错 - Can't solve this python problem I got error every time 为什么我的预测 model 每次运行时都会给出不同的预测结果? - Why does my prediction model keep giving a different prediction result every time I run it? 我查询我的 sqlite 数据库并得到结果为 [ ](空白),即使我的数据库有我试图查询的数据 - I query my sqlite database and got the result as [ ] (blank) even when my database has the data I tried to query 我试图创建我的第一个数据库,但出现错误 - I tried to make my first database and I got error 当我尝试在 python 中创建浏览器时出现错误 - I got an error when I tried to create a browser in python 我试图运行 python 脚本,但得到 ImportError:尝试相对导入,没有已知的父 package - I tried to run a python script, but got ImportError: attempted relative import with no known parent package 我尝试在ubuntu中安装python 3.6,但是每次在终端中给出命令时,都会显示错误: - I tried installing python 3.6 in ubuntu but every time I give the command in the terminal it shows an error: 我尝试安装 pyinstaller 并得到了这个 - i tried to install pyinstaller and got this 我可以在 python 终端上导入 tensorflow 但如果我尝试运行我的 py 文件我不能 - I can import tensorflow on python terminal but if i tried to run my py file i cant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM