如果您将输出文件打开为“wb”,那么它接受字节流而不是 unicode 参数:
s = 'слово'
with open('data.txt','wb') as f:
f.write(s.encode('unicode_escape'))
f.write(b'\n') # add a line feed
这似乎做你想做的:
$ cat data.txt
\u0441\u043b\u043e\u0432\u043e
它避免了解码以及将 unicode 写入文本流时发生的任何翻译。
根据@J.F.Sebastian 的建议更新为使用encode('unicode_escape')。
%timeit 报告它比encode('ascii', 'backslashreplace') 快很多:
In [18]: f = open('data.txt', 'wb')
In [19]: %timeit f.write(s.encode('unicode_escape'))
The slowest run took 224.43 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 1.55 µs per loop
In [20]: %timeit f.write(s.encode('ascii','backslashreplace'))
The slowest run took 9.13 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 2.37 µs per loop
In [21]: f.close()
奇怪的是,尽管每次循环时间更快,但编码('unicode_escape')的 timeit 的滞后比编码('ascii','backslashreplace') 的滞后要长得多,因此请务必在您的环境中测试两者。