1

这是我的代码(t.name 包含一个希伯来名字):

# -*- coding: utf8 -*-  
                title = '%s.html' % t.name
                with file(title, 'wb') as fpo:
                    fpo.write('<meta charset="utf-8">\n')                    
                    message = 'שלום לך %s' % t.name
                    fpo.write('%s\n' % message)

以下是文件在文件系统中的外观(Windows 7): 希伯来文文件名

内容由浏览器很好地呈现。

我在这里想念什么?

谢谢,奥马尔。

4

2 回答 2

2

Windows 文件系统使用 UTF16 编码。不过,最好的办法是使用unicode值,因为 Python 会自动为您的平台使用正确的编解码器和 API 来编码文件名:

title = u'%s.html' % t.name.decode('utf8')  # decode from UTF8, use a unicode literal
with file(title, 'wb') as fpo:
    fpo.write('<meta charset="utf-8">\n')                    
    message = 'שלום לך %s' % t.name
    fpo.write('%s\n' % message)
于 2013-08-05T14:04:37.303 回答
0

尝试title=u'%s.html' % t.name

于 2013-08-05T13:25:14.283 回答