This question already has an answer here:
这个问题在这里已有答案:
- Reading a UTF8 CSV file with Python 7 answers
使用Python 7答案读取UTF8 CSV文件
i am using google app engine to import a csv file and insert it into a database but its giving me this error:
我正在使用谷歌应用程序引擎导入一个csv文件并将其插入数据库,但它给我这个错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf0' in position 3: ordinal not in range(128)
the file i'm importing is utf-8 the python code is utf-8
我导入的文件是utf-8,python代码是utf-8
heres the form:
继承人的形式:
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))
And here is the post class:
这是帖子类:
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 个解决方案
#1
0
This should work - self.request.get('file').decode('utf-8').encode('ascii', 'ignore')
这应该工作 - self.request.get('file')。decode('utf-8')。encode('ascii','ignore')
#1
0
This should work - self.request.get('file').decode('utf-8').encode('ascii', 'ignore')
这应该工作 - self.request.get('file')。decode('utf-8')。encode('ascii','ignore')