简体   繁体   中英

How can I change only one letter of .mp3 in python?

So I have to change the letter "ı" because it is not an english character for example I should change "sanmayın.mp3" to "sanmayin.mp3". How can I do this?

from os import rename, listdir

fnames = listdir('.')


for fname in fnames:
    print fname
    fname.replace('ý','i')

Okay I got why it didn't work listdir gives all of the names in english so python thinks that ı is i, how can I get it to work in UTF-8

Use str.replace() :

s = 'sanmayın.mp3'
s.replace('ı','i')

Result :

sanmayin.mp3

If you needed to replace characters within a list:

l = ['sanmayın.mp3', 'ın', 'hı']
l = [i.replace('ı','i') for i in l] 

Result :

['sanmayin.mp3', 'in', 'hi']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM