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?
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.