fork download
  1. # http://stackoverflow.com/q/40122058/5290909
  2. import re
  3.  
  4. s = "You say: 你好. I say: 再見. 答案, my friend, 在風在吹"
  5. utf_line = s.decode('utf-8')
  6.  
  7. dict = {"你好" : "hello",
  8. "再見" : "goodbye",
  9. "答案" : "The answer",
  10. "在風在吹" : "is blowing in the wind",
  11. }
  12.  
  13. def translate(m):
  14. block = m.group().encode('utf-8')
  15. if block in dict:
  16. return dict[ block ]
  17. else:
  18. return "----"
  19.  
  20.  
  21. utf_translated = re.sub(ur'[\u4e00-\u9fff]+', translate, utf_line, re.UNICODE)
  22.  
  23. print utf_translated.encode('utf-8')
Success #stdin #stdout 0.02s 9120KB
stdin
Standard input is empty
stdout
You say: hello. I say: goodbye. The answer, my friend, is blowing in the wind