python中文入库
http://www.cnblogs.com/evening/archive/2012/04/19/2457440.html
工作需要,将cvm.csv文件中的记录志入数据库, name, owner包含中文,python2.7仅支持unicode,所以文件中读出的中文需要解码(decode)一下,才能写入mysql数据库。
# file cvm.csv
cvm.csv: UTF-8 Unicode text
writedb.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from app import db
from app.models import Server
import uuid
with open('cvm.csv', 'r') as f:
for line in f:
sn, name, public, private, owner = line.strip().split(',')
server = Server()
server.uuid = str(uuid.uuid4())
server.sn = sn.decode('utf-8')
server.name = name.decode('utf-8')
server.public = public
server.private = private
server.owner = owner.decode('utf-8')
db.session.add(server)
db.session.commit()
posted on
2017-05-10 18:05
北京涛子 阅读(
...) 评论(
)
编辑
收藏
转载于:https://www.cnblogs.com/liujitao79/p/6837389.html