Python string.replace() not replacing characters

后端 未结 5 1125
粉色の甜心
粉色の甜心 2020-12-06 05:43

Some background information: We have an ancient web-based document database system where I work, almost entirely consisting of MS Office documents with the \"normal\" extens

5条回答
  •  伪装坚强ぢ
    2020-12-06 06:20

    You are starting over with the base line instead of saving the replaced result, thus you are getting the equivalent to

    filename = line[2].replace('*', '_')
    foldername = line[5].replace('*', '_')
    

    Try the following

    bad_characters = ["/", "\\", ":", "(", ")", "<", ">", "|", "?", "*"]
    filename = line[2]
    foldername = line[5]
    for letter in bad_characters:
        filename = filename.replace(letter, "_")
        foldername = foldername.replace(letter, "_")
    

提交回复
热议问题