[英]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.