-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrans.py
45 lines (44 loc) · 1.29 KB
/
trans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 7/23/2018 4:53 PM
# @Author : SkullFang
# @Contact : [email protected]
# @File : trans.py
# @Software: PyCharm
import sys,getopt
from googletrans import Translator
import time
def main(argv):
input= ''
output= ''
sinstance=''
translator = Translator(service_urls=['translate.google.cn'])
try:
opts, args = getopt.getopt(argv,"s:hi:o:",["s=","i=","o="])
except getopt.GetoptError:
print('test.py -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('test.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ('-s',"--s"):
sinstance=arg
print(translator.translate(
sinstance, src='zh-cn', dest='en').text.strip('\n'))
sys.exit()
elif opt in ("-i", "--i"):
input = arg
elif opt in ("-o", "--o"):
output = arg
fin = open(input[1:-1], 'r', encoding='utf-8')
fou = open(output[1:-1], 'w', encoding='utf-8')
for line in fin:
print(line)
fou.write(line)
text = translator.translate(line, src='zh-cn', dest='en').text.strip('\n')
print(text + '\n')
fou.write(text + '\n')
time.sleep(1)
if __name__ == "__main__":
main(sys.argv[1:])