Google Cloud Storage API写入具有特殊字符的文件与常规python文件 [英] Google Cloud Storage API write files with special characters vs regular python files
问题描述
我正在使用Google App Engine将新文件写入Google Cloud Storage存储桶,以最终在浏览器中提供服务。通常,在本地计算机上,这会写一个漂亮的文本文件,我可以打开该文件并按预期查看测试字符:
I am using Google App Engine to write a new file to a Google Cloud Storage bucket for eventual serving in the browser. Normally on my local computer this writes a nice text file which I can open and see the test character as expected:
with open('new_file.txt', 'w') as f:
f.write(u'é'.encode('utf-8'))
在记事本中打开 new_file.txt
时,它会正确显示为é
。
When I open new_file.txt
in Notepad it's properly displayed as é
.
但是当我在Google Cloud Storage上尝试类似过程时:
But when I try the analogous process on Google Cloud Storage:
with gcs.open('/mybucket/newfile.txt', 'w', content_type='text/html') as f:
f.write(u'é'.encode('utf-8'))
我的文件在浏览器中的特殊字符都被弄乱了,在在这种情况下,它输出é
。
My files are served in the browser with special characters all messed up, in this case it outputs é
.
推荐答案
HTTP 1.1的默认字符集为ISO-8859-1。
The default charset for HTTP 1.1 is ISO-8859-1.
如果您希望浏览器将文本解释为UTF-8,则应将content-type标头设置为包含字符集,如下所示:
If you want the browser to interpret your text as UTF-8, you should set the content-type header to include the charset, like this:
with gcs.open('/mybucket/newfile.txt', 'w', content_type='text/html; charset=utf-8') as f:
这篇关于Google Cloud Storage API写入具有特殊字符的文件与常规python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!