Create (sane/safe) filename from any (unsafe) string

前端 未结 11 1317
醉酒成梦
醉酒成梦 2020-12-28 12:45

I want to create a sane/safe filename (i.e. somewhat readable, no \"strange\" characters, etc.) from some random Unicode string (mich might contain just anything).

(

11条回答
  •  既然无缘
    2020-12-28 13:26

    Another approach is to specify a replacement for any unwanted symbol. This way filename may look more readable.

    >>> substitute_chars = {'/':'-', ' ':''}
    >>> filename = 'Cedric_Kelly_12/10/2020 7:56 am_317168.pdf'
    >>> "".join(substitute_chars.get(c, c) for c in filename)
    'Cedric_Kelly_12-10-20207:56am_317168.pdf'
    

提交回复
热议问题