BlueBlue
BlueBlue

Reputation: 101

Replacing a character in a string using its unicode Python

I want to replace the character ي (Unicode:\u064A) With the character ی (Unicode:\u06CC). I tried the following code:

import unicodedata as ud
normalForm = ud.normalize('NFKD','رزاﻗﻲ')
for charr in normalForm:
  if charr == 'ي':
    print('true')
    charr = charr.replace('ي','ی')
print(normalForm)

However, this code doesn't work, and it returns the same character:

رزاقي

while it should return:

رزاقی

I wanted to try replacing using their Unicode (or any other method), How can I achieve this?

Upvotes: 0

Views: 144

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 178115

Call .replace directly on the string. Python string are immutable, so you have to assign the return value to retain the change as well:

import unicodedata as ud

original = 'رزاﻗﻲ'
nfkd = ud.normalize('NFKD',original)
replaced = nfkd.replace('ي','ی')
print('orig',ascii(original),original)
print('nfkd',ascii(nfkd),nfkd)
print('repl',ascii(replaced),replaced)

Output with ASCII representation since visually they look the same:

orig '\u0631\u0632\u0627\ufed7\ufef2' رزاﻗﻲ
nfkd '\u0631\u0632\u0627\u0642\u064a' رزاقي
repl '\u0631\u0632\u0627\u0642\u06cc' رزاقی

Upvotes: 1

Related Questions