1. 程式人生 > >python讀取sqlserver資料,並儲存到csv中

python讀取sqlserver資料,並儲存到csv中

# -*- coding: utf-8 -*-
"""
@use:查詢17.11-18.1,18.6-18.8的PM2.5資料,匯出到csv
"""
import pymssql
import xlwt
import datetime

from xml.dom.minidom import parse 

def do(): 
    try:
        doc=parse('baseinfo.xml')
        login=doc.getElementsByTagName("login")[0]
        ip=login.getAttribute("ip")
        user=login.getAttribute("user")
        password=login.getAttribute("password")
        database=login.getAttribute("database")
        
        timerange=doc.getElementsByTagName("timerange")[0]
        start=timerange.getAttribute("start")
        end=timerange.getAttribute("end")  
        
        start1 =datetime.datetime.strptime(start, '%Y-%m-%d %H:%M:%S')
        end1 = datetime.datetime.strptime(end, '%Y-%m-%d %H:%M:%S')
        theHour=int((end1-start1).days*24 + (end1-start1).seconds/3600)+1
        print(theHour)
    
            
        conn=pymssql.connect(host=ip,user=user,password=password,database=database)
        #檢視連線是否成功
        cursor = conn.cursor()
        #    sql = "select a.STATIONCODE,a.MONIDATE,b.LONGITUDE,b.LATITUDE,a.PM25  from T_MON_AIRSTATIONHOUR a  join T_BAS_AIRSTATION b  on a.STATIONCODE=b.STATIONCODE where MONIDATE='2018-09-21 15:00:00.000'"
        #日期區間資料查詢
        #sql = "select a.STATIONCODE,a.MONIDATE,b.LONGITUDE,b.LATITUDE,a.PM25  from T_MON_AIRSTATIONHOUR a  join T_BAS_AIRSTATION b  on a.STATIONCODE=b.STATIONCODE where MONIDATE>='"+start+"' and MONIDATE<='"+end+"'"
        
        for h in range(theHour):
            monidate=(start1 + datetime.timedelta(hours=h)).strftime('%Y-%m-%d %H:%M:%S')
            #        print(monidate)
            #單個時間資料查詢
#            sql = "select a.STATIONCODE,a.MONIDATE,b.LONGITUDE,b.LATITUDE,a.PM25  from T_MON_AIRSTATIONHOUR a  join T_BAS_AIRSTATION b  on a.STATIONCODE=b.STATIONCODE where MONIDATE='"+monidate+"'"
            sql="select a.ID,a.RecordTime,b.LNG1,b.LAT1,a.PM25 from T_MIC_DATAAIR a  join T_MIC_STATION b  on a.ID=b.ID where b.STATIONTYPE='2' and a.RecordTime='"+monidate+"'"
            #時間列,轉換成 2018-09-21T15:00:00  格式
            str_date=str(datetime.datetime.strptime(str(monidate), "%Y-%m-%d %H:%M:%S") )
            str_date=str_date[0:10]+'T'+str_date[-8:] 
            cursor.execute(sql)
            #用一個rs變數獲取資料
            rs = cursor.fetchall()
            #建立檔案
            file = xlwt.Workbook()
            #寫入sheet名稱,2017-05-01T06_00_00  格式
            name_sheet=str_date[0:10]+'T'+str_date[-8:-6]+'_00_00'
            table = file.add_sheet(name_sheet)
        
            title=['deviceid','date','lon','lat','pm25']
            #table.write(行_0開始, 列_0開始, 值)
            #寫入表頭,第一行
            for j in range(5):
                table.write(0, j, title[j])
            #index_i+1 行,index_j列
            for index_i,r in enumerate(rs):
                for index_j,val in enumerate(r):
                    if index_j== 1:
                        
                        val1=str(datetime.datetime.strptime(str(val), "%Y-%m-%d %H:%M:%S") )
                        str1=val1[0:10]+'T'+val1[-8:]
                        table.write(index_i+1, index_j, str1)
        #                '2018-09-21T15:00:00'
                    else:
                        table.write(index_i+1, index_j, val)
            #儲存檔案,2018-09-21T15_00_00.csv 名稱格式
            name_file=name_sheet+'.csv'
            file.save(name_file)

    except Exception as e:
        print(e)
        cursor.close() 
        conn.close()
        return
    
    cursor.close() 
    conn.commit()      
    conn.close() 
            
    

if __name__ == '__main__':

     do()

 

 

Python3操作SQL Server資料庫(例項講解)

https://www.jb51.net/article/126414.htm

1.xlwt

 

#輸出資料

# for r in rs:

# print(len(r))

# for str in r:

# print(str)

# print(rs)

 

  1. python Excel資料讀取與寫入

import xlwt

 

file = xlwt.Workbook()

 

table = file.add_sheet('sheet name')

for i in range(10):

    table.write(i, 0, i)

 

file.save('xls_save.xls')

https://blog.csdn.net/qq_35451572/article/details/80427960

 

3.python for迴圈訪問陣列下標

http://outofmemory.cn/code-snippet/3741/accessing-the-index-in-python-for-loops

但是有時候我們會需要在便利陣列的同時訪問下標,這時候可以藉助於enumerate函式來實現,例如:

l = [1,2,3]for index,val in enumerate(l):

    print 'index is %d, val is %d' % (index,val)

#輸出下標

# for index_i,r in enumerate(rs):

# print(index_i)

# for index_j,str in enumerate(r):

# print(index_j)

 

4.Python if 語句

http://www.runoob.com/python3/python3-if-example.html

以下例項通過使用 if...elif...else 語句判斷數字是正數、負數或零:

例項(Python 3.0+)

 num = float(input("輸入一個數字: ")) 

if num > 0: print("正數") 

elif num == 0: print("") 

else: print("負數")

  1. 日期格式轉換及擷取字串

日期最終格式是

2018-09-21T15:00:00

a】Python 日期格式轉換

times = time.strftime("%H.%M", time.strptime(time_string, formats)) #將時間轉為hh.mm型別 

ftime = datetime.datetime.strptime(times, "%H.%M") #將times字串轉為%H.%M的datetime型別

 

datetime.datetime.strptime(str(val), "%Y-%m-%d %H:%M:%S")

【b】python中的字元數字之間的轉換函式

https://www.cnblogs.com/wuxiangli/p/6046800.html

str(x )                 將物件 x 轉換為字串   

c】擷取字串

https://www.cnblogs.com/huangcong/archive/2011/08/29/2158268.html

str = ’0123456789′
print str[0:3] #擷取第一位到第三位的字元
print str[:] #擷取字串的全部字元
print str[6:] #擷取第七個字元到結尾
print str[:-3] #擷取從頭開始到倒數第三個字元之前
print str[2] #擷取第三個字元
print str[-1] #擷取倒數第一個字元
print str[::-1] #創造一個與原字串順序相反的字串
print str[-3:-1] #擷取倒數第三位與倒數第一位之前的字元
print str[-3:] #擷取倒數第三位到結尾
print str[:-5:-3] #逆序擷取,具體啥意思沒搞明白?

 

【d】Python 字串操作(string替換、刪除、擷取、複製、連線、比較、查詢、包含、大小寫轉換、分割等)

https://www.cnblogs.com/exusll/p/6393621.html?utm_source=tuicool&utm_medium=referral

【5】  TypeError: 'datetime.datetime' object is not subscriptable

報錯行:  str1=val1[0:10]+'T'+val1[-8:]

    for index_i,r in enumerate(rs):

        for index_j,val in enumerate(r):

            if index_j== 1:

                

                val1=str(datetime.datetime.strptime(str(val), "%Y-%m-%d %H:%M:%S") )

                str1=val1[0:10]+'T'+val1[-8:]

                table.write(index_i+1, index_j, str1)

  1. 讀取配置檔案

from xml.dom.minidom import parse

【1】連線資料庫

    conn = pymssql.connect(host='.',

                user='sa',

                password='123456',

                database='AirDB_CC',

                charset='utf8')

改成

doc=parse('baseinfo.xml')

    login=doc.getElementsByTagName("login")[0]

    ip=login.getAttribute("ip")

    user=login.getAttribute("user")

    password=login.getAttribute("password")

database=login.getAttribute("database")

conn=pymssql.connect(host=ip,user=user,password=password,database=database)

【2】讀取日期

    <timerange start="2018-09-13 06:00" end="2018-09-13 08:00"></timerange>

 

        timerange=doc.getElementsByTagName("timerange")[0]

        start=timerange.getAttribute("start")

        end=timerange.getAttribute("end")  

【3】計算小時差

【a】

https://blog.csdn.net/u012062455/article/details/73287023/

secondsDiff=(dataTimea-dataTimeb).seconds

【b】

[Python3]顯示當前時間、計算時間差以及時間加減法

https://blog.csdn.net/asher117/article/details/83012803

【c】

Python3基礎知識之日期時間與字元的轉換

http://www.cnblogs.com/johsan/p/9046406.html

【d】

python 日期格式和字串格式的轉化

https://www.cnblogs.com/he0xff/p/8479499.html

【e】

Python學習筆記 --- 迴圈遍歷時間區間

https://blog.csdn.net/u012965373/article/details/78060533

 

    start1 =datetime.datetime.strptime(start, '%Y-%m-%d %H:%M:%S')

    end1 = datetime.datetime.strptime(end, '%Y-%m-%d %H:%M:%S')

print(int((end1-start1).seconds/3600))

【4】遍歷小時

theHour=int((end1-start1).seconds/3600)+1

print(theHour)

for h in range(theHour):

monidate=(start1 +datetime.timedelta(hours=h)).strftime('%Y-%m-%d %H:%M:%S')

print(monidate)

輸出:

3

2018-09-13 06:00:00

2018-09-13 07:00:00

2018-09-13 08:00:00

 

【5】try

 

 

  1. (end1-start1).seconds跨越天數的情況

        theHour=int((end1-start1).days*24 + (end1-start1).seconds/3600)+1

        print(theHour)

  1. 日資料匯出到csv

 

day = begin + datetime.timedelta(days=i)

 

T_MIC_DATAAIRDAY  日資料表

<timerange start="2018-06-01 00:00:00" end="2018-08-31 23:00:00"></timerange>

【1】time data '2018-06-01' does not match format '%Y-%m-%d %H:%M:%S'

monidate=(start1 + datetime.timedelta(days=h)).strftime('%Y-%m-%d %H:%M:%S')

改成

            monidate=(start1 + datetime.timedelta(days=h)).strftime('%Y-%m-%d')

【2】time data '2018-06-01' does not match format '%Y-%m-%d %H:%M:%S'

str_date=str(datetime.datetime.strptime(str(monidate), "%Y-%m-%d %H:%M:%S") )

            str_date=str_date[0:10]+'T'+str_date[-8:]

改成

str_date=str(monidate)[0:10]+'T'+'00:00:00'

 

【3】time data '2018-06-01' does not match format '%Y-%m-%d %H:%M:%S'

val1=str(datetime.datetime.strptime(str(val), "%Y-%m-%d %H:%M:%S") )

                        str1=val1[0:10]+'T'+val1[-8:]

                        table.write(index_i+1, index_j, str1)

改成

table.write(index_i+1, index_j, str_date)