转换“/”成“/”

来源:互联网 发布:没有系统的嵌入式编程 编辑:程序博客网 时间:2024/04/24 10:30

编程过程中经常涉及到文件操作,要将路径中的"/"改成“/”,很麻烦

今天试着自己写个脚本,嗯,果然没想想中的简单,但还是在各位网友的帮助下解决了,在此分享下,虽然很简单,却真的让我体验到了乐趣,嘿嘿

 

escape_dic={'/a':r'/a','/b':r'/b','/c':r'/c',
           '/f':r'/f',
           '/n':r'/n',
           '/r':r'/r',
           '/t':r'/t',
           '/v':r'/v',
           '/'':r'/'',
           '/"':r'/"',
           '/0':r'/0',
           '/1':r'/1',
           '/2':r'/2',
           '/3':r'/3',
           '/4':r'/4',
           '/5':r'/5',
           '/6':r'/6',
           '/7':r'/7',
           '/8':r'/8',
           '/9':r'/9'}
def tran(sou):
    new_string=''
    for de in sou:
       
        try: new_string+=escape_dic[de]
        except KeyError: new_string+=de
       
    return new_string.replace('//', '/')