2016-06-17 1 views
1

파이썬에서 Google Translate API를 사용하여 전체 .txt 파일을 번역하려고합니다.Python에서 Google Translate API를 사용하여 전체 .txt 파일을 번역하는 방법은 무엇입니까?

기본 코드는 다음과 같습니다

print(service.translations().list(source='zh', 
            target='en', 
            q=['上海熙缘节能环保科技服务有限公司', 
            '广东宏德科技物业有限公司']).execute()) 

if __name__ == '__main__': 
    main() 
는 .txt 파일을 변환 할 수 있도록

가 어떻게이 코드를 수정할 수 있습니다?

print(service.translations().list(source='zh', target='en', q= (line.rstrip() for line in open('file.txt')).execute()) 

내가 표현식을 참조하십시오

+0

파일에서 텍스트를 읽는 방법을 묻고 있습니까? –

+0

예 (중국어 문자 포함)이므로 파일은 UTF 또는 유니 코드 형식입니다. 기본 디코더는 cp1252입니다. – shj997

+0

가능한 [Google 번역을 통해 번역 할 Python 스크립트] (http://stackoverflow.com/questions/9404628/python-script-to-translate-via-google-translate) –

답변

0

복잡한 세부 사항을 모르고 내 자신의 관점에서 간단하고 효율적인 솔루션 및 I 매개 변수 q 가정 문장/당신이 될 것입니다 번역 할 단어의 순서입니다 정말로 unpythonic하기 위해서, 당신은 하나의 단일 라인으로 많은 표현을 중첩합니다.

+0

감사합니다. direprobs, Google이 제공 한 것을 사용합니다. 이제 UnicodeDecodeError를 얻었습니다. 'charmap'코덱은 바이트 0x9d를 50 위치에서 디코딩 할 수 없습니다. 문자는 오류로 매핑됩니다. Google 번역의 API는 cp1252를 사용하지만 내 중국어 텍스트는 값을 유지하기 위해 유니 코드로 저장해야합니다. 그렇지 않으면 사라집니다. – shj997

+0

다음은 Google에서 Decoder 클래스로 사용하는 코드입니다. IncrementalDecoder (codecs.IncrementalDecoder) : def decode (self, input, final = False) : return codecs.charmap_decode (input, self.errors, decoding_table) [0 ] 클래스에서는 StreamWriter (코덱 codecs.StreamWriter) 패스 클래스에서는 StreamReader (코덱 codecs.StreamReader) 패스 ### 인코딩 모듈 API는 데프 getregentry() 창 codecs.CodecInfo ( name = 'cp1252', encode = Codec(). encode, – shj997

+0

@ shj997이 페이지 참조 Python 문서 : [Unicode HOWTO] (https://docs.python.org/3/howto/unicode.html). – direprobs

관련 문제