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– Job2012-05-10 20:15:49 +00:00Commented May 10, 2012 at 20:15
-
Putting a reply to a comment in the question is pointless. Just make it a comment.Dynamic– Dynamic2012-05-10 21:44:44 +00:00Commented May 10, 2012 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– Moked2012-05-10 22:06:07 +00:00Commented May 10, 2012 at 22:06
-
3For those voting to close to migrate to Stack Overflow, please consider - there's no code so it wouldn't survive there very long.ChrisF– ChrisF2012-05-10 22:09:10 +00:00Commented May 10, 2012 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– Casey Kuball2012-05-10 22:11:56 +00:00Commented May 10, 2012 at 22:11
2 Answers
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.