登峰造极境

  • WIN
    • CSharp
    • JAVA
    • OAM
    • DirectX
    • Emgucv
  • UNIX
    • FFmpeg
    • QT
    • Python
    • Opencv
    • Openwrt
    • Twisted
    • Design Patterns
    • Mysql
    • Mycat
    • MariaDB
    • Make
    • OAM
    • Supervisor
    • Nginx
    • KVM
    • Docker
    • OpenStack
  • WEB
    • ASP
    • Node.js
    • PHP
    • Directadmin
    • Openssl
    • Regex
  • APP
    • Android
  • AI
    • Algorithm
    • Deep Learning
    • Machine Learning
  • IOT
    • Device
    • MSP430
  • DIY
    • Algorithm
    • Design Patterns
    • MATH
    • X98 AIR 3G
    • Tucao
    • fun
  • LIFE
    • 美食
    • 关于我
  • LINKS
  • ME
Claves
长风破浪会有时,直挂云帆济沧海
  1. 首页
  2. Programming
  3. Python
  4. 正文

Python读Mysql数据并写入csv文件

2017-04-17

代码见下:

# -*- coding: UTF-8 -*-
import pymysql.cursors
import json
import csv

db = pymysql.connect("127.0.0.1", "root", "123456", "device",charset='utf8');
cursor = db.cursor()  #mysql cursor handle
sql = "select * from tempdata_20170417"
cursor.execute(sql)
csvfile = file('temp.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerow(['','channel1','channel2','channel3','channel4','channel5','channel6','channel7','channel8','channel9','channel10','channel11','channel12','channel13','channel14','channel15','channel16'])
for r in cursor:
    json_data =  json.loads(r[1]) #["channel1"]
    try:
        writer.writerow([r[0],
                     json_data["channel0"][0],
                     json_data["channel0"][1],
                     json_data["channel0"][2],
                     json_data["channel0"][3],
                     json_data["channel0"][4],
                     json_data["channel0"][5],
                     json_data["channel0"][6],
                     json_data["channel0"][7],
                     json_data["channel1"][0],
                     json_data["channel1"][1],
                     json_data["channel1"][2],
                     json_data["channel1"][3],
                     json_data["channel1"][4],
                     json_data["channel1"][5],
                     json_data["channel1"][6],
                     json_data["channel1"][7]
                     ])
    except:
        continue
csvfile.close()
db.close()

 

标签: 暂无
最后更新:2017-04-17

代号山岳

知之为知之 不知为不知

点赞
< 上一篇
下一篇 >

COPYRIGHT © 2099 登峰造极境. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

蜀ICP备14031139号-5

川公网安备51012202000587号