Fix: UnicodeEncodeError with python 2.x

refs #3
This commit is contained in:
ssut 2015-07-21 00:59:40 +09:00
parent 4d4f3ec41b
commit 5469d9fa81
1 changed files with 2 additions and 7 deletions

View File

@ -4,8 +4,6 @@ import argparse
import sys import sys
from googletrans import translator from googletrans import translator
PY3 = sys.version_info > (3, 1)
def main(): def main():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Python Google Translator as a command-line tool') description='Python Google Translator as a command-line tool')
@ -28,16 +26,13 @@ def main():
return return
result = translator.translate(args.text, dest=args.dest, src=args.src) result = translator.translate(args.text, dest=args.dest, src=args.src)
text = result.text if PY3 else result.text.encode('utf-8', 'ignore') result = u"""
pronunciation = result.pronunciation if PY3 else result.pronunciation.encode('utf-8', 'ignore')
result = """
[{src}] {original} [{src}] {original}
-> ->
[{dest}] {text} [{dest}] {text}
[pron.] {pronunciation} [pron.] {pronunciation}
""".strip().format(src=result.src, dest=result.dest, original=result.origin, """.strip().format(src=result.src, dest=result.dest, original=result.origin,
text=text, text=result.text, pronunciation=result.pronunciation)
pronunciation=pronunciation)
print(result) print(result)
if __name__ == '__main__': if __name__ == '__main__':