简体   繁体   English

content-type text / plain的文件扩展名为.ksh?

[英]content-type text/plain has file extension .ksh?

Python 2.7: Python 2.7:

>>> from mimetypes import guess_extension
>>> guess_extension('text/plain')
'.ksh'

Python 3.5: Python 3.5:

>>> from mimetypes import guess_extension
>>> guess_extension('text/plain')
'.c'

How can I get a valid answer? 我怎样才能得到有效答案?

For me ".txt" would fit. 对我来说,“。txt”适合。

Even the filetype lib can't handle this :-( 即使文件类型 lib也无法处理这个:-(

See https://github.com/h2non/filetype.py/issues/30 请参阅https://github.com/h2non/filetype.py/issues/30

To get consistent outputs with Python 3 and 2, you need to use guess_all_extensions and sort the output: 要使用Python 3和2获得一致的输出,您需要使用guess_all_extensions并对输出进行排序:

>>> from mimetypes import guess_all_extensions
>>> sorted(guess_all_extensions('text/plain'))
['.asc', '.bat', '.c', '.cc', '.conf', '.cxx', '.el', '.f90', '.h', '.hh', '.hxx', '.ksh', '.log', '.pl', '.pm', '.text', '.txt']

.txt is the last item. .txt是最后一项。

It's kinda odd these aren't already sorted since guess_extension just takes the first arbitrary extension , hence the different outputs you observe. 有点奇怪,这些尚未排序,因为guess_extension只采用第一个任意扩展 ,因此您观察到不同的输出。

although the question mentioned mimetypes.guess_extension , but it actually cannot be answered with the information in that module. 虽然问题提到了mimetypes.guess_extension ,但它实际上无法用该模块中的信息来回答。 mime type to extension mapping is one to multi, there is no weight info in the mimetypes database, sorting extensions by alphabetical order could give a consistent answer, but apparently not what OP wants. mime类型到扩展名映射是mimetypesmimetypes数据库中没有权重信息,按字母顺序排序扩展可以给出一致的答案,但显然不是OP想要的。 I considered the following options: 我考虑了以下选项:

  • by authority, IANA DB does not have extension information for every type, only a few types have this info and need hard work to parse. 根据权限, IANA DB没有针对每种类型的扩展信息,只有少数类型具有此信息并且需要努力解析。

  • by popularity, I hope there is one. 受欢迎程度,我希望有一个。

  • by consensus, an MDN wiki page named "Incomplete list of MIME types" is most close: it is actively maintained, it lists only one extension for some well-known mime type. 一致同意,名为“不完整的MIME类型列表”的MDN维基页面最为接近:它是主动维护的,它只为一些众所周知的mime类型列出了一个扩展名。

I guess the practical solution is, grab the table from the aforementioned MDN wiki, hard code those types, use mimetypes.guess_extension as a fallback. 我想实际的解决方案是,从上述MDN wiki中获取表格,硬编码这些类型,使用mimetypes.guess_extension作为后备。

note you should take care of MDN content license . 请注意,您应该注意MDN内容许可证

guess_extension just does something like: guess_extension就是这样的:

for x in mt.types_map:
    if mt.types_map[x] == 'text/plain': 
        return x

Since there are multiple extensions that can be associated with text files, there really is nothing better that can be done (without some a-priori knowledge, which may or may not make sense to most of us). 由于有多个扩展可以与文本文件相关联,因此没有什么比这更好的了(没有一些先验知识,这对我们大多数人来说可能有意义,也可能没有意义)。 As such, the only way to have this work are some of the workarounds on sorting or selecting manually the extension you want. 因此,完成此工作的唯一方法是有关排序或手动选择所需扩展的一些解决方法。 I would recommend just wrapping in a function that makes more sense - you are taking about a default extension (I think), not a guess. 我建议只包装一个更有意义的函数 - 你正在考虑默认扩展(我认为),而不是猜测。 As such, I would probably hard-code something like: 因此,我可能会硬编码:

def default_extension(type):
     if type == 'text/plain': return '.txt'
     return mt.guess_extension(type)

with an if for any default you want. if您想要任何默认值。 Maybe it would make more sense to have a defaults dictionary. 也许拥有默认字典会更有意义。

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

相关问题 将Content-Type标头转换为文件扩展名 - Convert Content-Type header into file extension 为什么在未发送请求时,wsgiref.simple_server将请求的内容类型报告为“文本/纯文本”? - Why does wsgiref.simple_server report content-type of request as 'text/plain' while none was sent? 下载带有 Content-Type': 'text/html 和 Content-Encoding': 'gzip' 的文件 - Download a file with Content-Type': 'text/html and Content-Encoding': 'gzip' 当Python可以时,为什么不能仅将Content-Type:text / html添加到文本文件中? - Why can't I just add Content-Type:text/html to a text file when Python can? Cherrypy和内容类型 - Cherrypy and content-type Django:多种内容类型:text / html出现在html页面上 - Django: Multiple Content-Type: text/html appears on html page 将不同内容类型的MHT文件提取到多个MHT文件中 - Extracting different content-type of MHT file into multiple mht file 将纯文本内容写入pdf文件 - Write content from plain text to pdf file 在Django中按内容类型验证文件的更好方法 - Better Way to validate file by content-type in django 使用“application / octet-stream”内容类型上传Django文件 - Django file upload with “application/octet-stream” content-type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM