Writing Unicode text to a text file?

前端 未结 8 2071
眼角桃花
眼角桃花 2020-11-22 16:46

I\'m pulling data out of a Google doc, processing it, and writing it to a file (that eventually I will paste into a Wordpress page).

It has some non-ASCII symbols. H

8条回答
  •  我在风中等你
    2020-11-22 17:26

    In Python 2.6+, you could use io.open() that is default (builtin open()) on Python 3:

    import io
    
    with io.open(filename, 'w', encoding=character_encoding) as file:
        file.write(unicode_text)
    

    It might be more convenient if you need to write the text incrementally (you don't need to call unicode_text.encode(character_encoding) multiple times). Unlike codecs module, io module has a proper universal newlines support.

提交回复
热议问题