简体   繁体   English

Windows XP上的Python unicode问题

[英]Python unicode problems on Windows XP

Having the following django view code that generates a CSV response from a database view: 具有以下从数据库视图生成CSV响应的django视图代码:

def _get_csv_stats(request, **filterargs):
   result = GlobalStats.objects.select_related().filter(**filterargs).values_list('user__username',
                                                                                  'user__first_name','user__last_name',
                                                                                  'center__name', 'action_name',
                                                                                  'action_date').annotate(num = Count('id')).order_by("action_date")
   response = HttpResponse(mimetype = 'text/csv')
   response.write( codecs.BOM_UTF8 )
   response['Content-Disposition'] = 'attachment; filename=statistcs.csv'
   writer = UnicodeWriter(response)
   for value in result:
       writer.writerow([unicode(v) for v in value])
   return response

Some of the columns contain utf8 text. 一些列包含utf8文本。 When I download the file and open it using Linux or Mac OS XI can see the text properly. 当我下载文件并使用Linux或Mac OS打开它时,XI可以正确看到文本。 But downloading and opening the file using windows XP strange characters appear in the place of the non-ASCII text. 但是,使用Windows XP下载和打开文件的奇怪字符会出现在非ASCII文本的位置。 Converting the file from csv to xls using Open Office on linux and opening the file on windows xp will result with a readable file (no strange characters). 使用Linux上的Open Office将文件从csv转换为xls并在Windows xp上打开文件将得到可读的文件(没有奇怪的字符)。

I can't see what I'm missing here as I don't work with win XP. 我看不到我在这里缺少什么,因为我不使用Win XP。 Has anyone experienced anything similar? 有没有人经历过类似的经历?

The UnicodeWriter class I'm using is descibed here at the bottom. 我使用的UnicodeWriter类descibed 这里在底部。

Whether the text is displayed properly depends on whether the font that is is being drawn in supports all Unicode characters. 文本是否正确显示取决于所绘制的字体是否支持所有Unicode字符。 This is not a problem with your django. 这不是您的django的问题。

First: I don't know django, so maybe this is far off. 第一:我不知道django,所以也许这还很遥远。

Check whether your writer actually outputs UTF-8, if you write an UTF-8 BOM into the file. 如果您将UTF-8 BOM写入文件,请检查您的编写器是否实际输出UTF-8。 Unicode and UTF-8 are not the same. Unicode和UTF-8不一样。 Also, afaikt, does Windows Notepad understand a UTF-8 BOM, so you getting "strange" characters can mean two things: 同样,afaikt,Windows记事本是否理解UTF-8 BOM,所以您得到“奇怪”字符可能意味着两件事:

a) the rest of the file is not really UTF-8-encoded, try v.encode('utf-8') for that a)文件的其余部分不是真正的UTF-8编码, v.encode('utf-8')尝试v.encode('utf-8')

b) the characters your trying to display are not supported by the font b)字体不支持您尝试显示的字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM