导入csv文件时出现unicode错误

时间:2013-12-03 16:45:38

标签: python google-app-engine csv utf-8

我使用谷歌应用程序引擎导入一个csv文件并将其插入数据库但它给我这个错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xf0' in position 3: ordinal not in range(128)

我导入的文件是utf-8 python代码是utf-8

表示形式:

    site = "order"

    scripts = ""

    content = """
        <div class="row" style="margin-top: 10px;">
            <div class="large-12 columns">
                <form enctype="multipart/form-data" method="post" action="import">
                    <select name="db">
                        <option value="users">Users</option>
                        <option value="machines">Machines</option>
                        <option value="company">Company</option>

                        <option value="process">Order</option>
                        <option value="status">Status</option>
                        <option value="part">Part</option>

                        <option value="machineType">Machine Type</option>
                    <select>
                    <input name="file" type="file">
                    <input type="submit" value="submit" class="button">
                </form>
            </div>
        </div>
    """

    template_values = {
        'site': site,
        'scripts': scripts,
        'content': content,
    }


    template = JINJA_ENVIRONMENT.get_template('main.html')
    self.response.write(template.render(template_values))

这是帖子类:

reader = csv.reader(StringIO.StringIO(self.request.get('file').decode('utf-8'))),

database = self.request.get('db')

for row in reader:
            for item in row:
                tiles = item[0].replace('&comma', ',').split(';')

                x = machineType(
                  Description = tiles[0],
                  ID = tiles[1],
                  Brand = tiles[2],
                  Type = tiles[3])
                x.put()

                self.response.write("Row imported [" + tiles[0] + ", "  + tiles[1] + ", "  + tiles[2] + ", "  + tiles[3] + "]</br>" )

1 个答案:

答案 0 :(得分:0)

这应该有效 - self.request.get('file').decode('utf-8').encode('ascii', 'ignore')