I need to transliterate a Hebrew text. So wrote a little Python script, which reads-in the Hebrew text from a text file, globally replaces the Hebrew letters with Latin ones. Now I need to do some fine tuning (replace certain things in some position but not in other, so a global replace won't do). The trouble is the following: The read-in text is a string so I cannot replace individual string elements. So, what would be the best way to do it?

  • Please provide examples. I do not speak Hebrew and have no idea what exactly your problem is (but could possibly help), so please elaborate. This is potentially the wrong place to ask this question. – Job May 10 '12 at 20:15
  • Putting a reply to a comment in the question is pointless. Just make it a comment. – Dynamic May 10 '12 at 21:44
  • @Job: Providing a Hebrew example would just complicate things. And btw., I am sure this is the right place to ask. The question is actually completely independent from my specific application. The question more general is: I have a string (my text) and I want to replace certain specific 'elements' (the letters) by some other 'elements'. How can I achieve this? – Moked May 10 '12 at 22:06
  • 3
    For those voting to close to migrate to Stack Overflow, please consider - there's no code so it wouldn't survive there very long. – ChrisF May 10 '12 at 22:09
  • >>> (replace certain things in some position but not in other, so a global replace won't do) What determines exactly which elements should and should not be replaced? Is it possible that matching a regular expression against your string can do this? – Casey Kuball May 10 '12 at 22:11

Convert your string into a list. Either a list of letters or (if this works in your case) a list of words. This is a basic approach but should work if your string is stored in writing order (rather than display order).

text_as_list = list(text_as_string)
replace_letters(text_as_list)  # this is the bit you're implementing
text_as_string = "".join(text_as_list)

If you are needing to pull the ";" out of "Hebrew W;rd" rather than writing a list and iterating through it, just find the replacing character and replace it.

If the conversion from Hebrew to Latin leaves ";" where "x" should be, loop through and replace the characters.

while ";" in converted_string:
     converted_string = converted_string.replace(";","x")

If some ";" are legit within the string, than just require conditions before executing the code to narrow down what bad characters to replace.

while ";" in converted_string:
      if converted_string[converted_string.find(";")-1] != "z": # sample condition
           converted_string = converted_string.replace(";","x") 

The real trick is not whether to use lists or strings, but writing the conditions well enough that parsing the code will be accurate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.