flask_admin csv 导出 中文乱码问题

# flask_admin csv 导出 中文乱码问题

相关参考 python 中文写入 CSV 乱码解决 (opens new window)

  • 原因:csv 文件开头缺少 BOM_UTF8 字符
  • 解决:在 Response 开头加上 BOM_UTF8 字符
  • 核心代码
titles[0] = codecs.BOM_UTF8.decode("utf8")+codecs.BOM_UTF8.decode()+titles[0]    
1
  • 相关引入
import codecs    
import csv    
    
from flask_admin._compat import csv_encode    
from flask import request, redirect, flash, current_app, Response, stream_with_context    
1
2
3
4
5
  • 逻辑代码(复制 放到对应的 ModelView 类里就好了)
    def _export_csv(self, return_url):    
        """    
            Export a CSV of records as a stream.    
        """    
        count, data = self._export_data()    
    
        # https://docs.djangoproject.com/en/1.8/howto/outputting-csv/    
        class Echo(object):    
            """    
            An object that implements just the write method of the file-like    
            interface.    
            """    
            def write(self, value):    
                """    
                Write the value by returning it, instead of storing    
                in a buffer.    
                """    
                return value    
    
        #    
        writer = csv.writer(Echo())    
    
        def generate():    
            # Append the column titles at the beginning    
            titles = [csv_encode(c[1]) for c in self._export_columns]    
            titles[0] = codecs.BOM_UTF8.decode("utf8")+codecs.BOM_UTF8.decode()+titles[0]    
            yield writer.writerow(titles)    
    
            for row in data:    
                vals = [csv_encode(self.get_export_value(row, c[0]))    
                        for c in self._export_columns]    
                yield writer.writerow(vals)    
    
        filename = self.get_export_name(export_type='csv')    
    
        disposition = 'attachment;filename=%s' % (secure_filename(filename),)    
    
        return Response(    
            stream_with_context(generate()),    
            headers={'Content-Disposition': disposition},    
            mimetype='text/csv'    
        )    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
上次更新: 2024-01-10 16:01:00
最近更新
01
自建 RSS 订阅
01-05
02
搭建私人 docker hub
01-02
03
关于 SSH 攻击
10-12
更多文章>