Python Requests URL with Unicode Parameters

后端 未结 3 2111
闹比i
闹比i 2021-01-06 07:25

I\'m currently trying to hit the google tts url, http://translate.google.com/translate_tts with japanese characters and phrases in python using the requests library.

3条回答
  •  误落风尘
    2021-01-06 08:10

    I made this little method before to help me with UTF-8 encoding. I was having issues printing cyrllic and CJK languages to csvs and this did the trick.

    def assist(unicode_string):
        utf8 = unicode_string.encode('utf-8')
        read = utf8.decode('string_escape')
    
        return read   ## UTF-8 encoded string
    

    Also, make sure you have these two lines at the beginning of your .py.

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    

    The first line is just a good python habit, it specifies which compiler to use on the .py (really only useful if you have more than one version of python loaded on your machine). The second line specifies the encoding of the python file. A slightly longer answer for this is given here.

提交回复
热议问题