Reputation: 179
I'm on Windows and Windows does not like the characters '?/"|:*<>' for folder-names. Now I wan't to replace said characters with '_'. It seems to work if I .replace(':', '_')
but not with any other character. But anyway, I want to replace all the above characters. I tried (somestring).replace(':', '_').replace('?', '_')
but it does not work.
How it is right now:
with open(unidecode(somestring).replace(':', '_')+'/{0}_{1}.txt'.format(counter, points), 'w+', encoding='utf-8') as outfile: outfile.write('{0}\n\n{1}\n'.format(stringhere, somecontent))
As said, it replaces the ':' just fine. But no other character. How can I replace multiple characters in this case?
Upvotes: 0
Views: 111
Reputation: 717
This should work:
res = ''.join(['_' if letter in '?/"|:*<>' else letter for letter in fe])
print(res)
#__________abcdefg______
Upvotes: 1
Reputation: 5958
Use regex
import re
fe = '?/"|:*<>?/abcdefg"|:*<>'
ke = re.sub(r'[?/"|:*<>]', '_', fe)
>>> fe
'?/"|:*<>?/abcdefg"|:*<>'
>>> ke
'__________abcdefg______'
Upvotes: 1