1. 程式人生 > >python解析csv格式資料入庫

python解析csv格式資料入庫

【1】匯入csv包

import csv

【2】讀取csv檔案路徑

def getExcelData():
    try:
     csvx_list = glob.glob(r'F:\公司專案\2018年專案\中加app\資料\中加資料\校準資料\*.csv')
     print('總共發現%s個csv檔案'% len(csvx_list))
     return csvx_list
    except Exception as e:
     print(e)

【3】開啟檔案,返回內容

def open_excel(file):
     try:
         data=csv.reader(open(file,'r'))
         #print(list(data))
         return data
     except Exception as e:
          print(e)

【4】獲取到的資料是list格式,遍歷資料入庫,擷取list

    for e in excels:
        data=open_excel(e)

        exceldata= list(data)[1:11]

#下標從1開始,截止到第11位,共計10位資料
       try:
            for value in exceldata:
                code= value[0] 
                datestr=value[1]
                pm25=float(value[4])

data:<_csv.reader object at 0x000000000C6A7A70>

list(data):

[['deviceid', 'date', 'lon', 'lat', 'pm25'], ['A1YQ520C02AC0510', '2018-03-19T17:00:00', '116.1406', '40.1375', '40.0'], ['A1YQ520201DE0512', '2018-03-19T17:00:00', '116.1318', '40.1373', '46.0'], ['A1YQ52090239051E', '2018-03-19T17:00:00', '116.1375', '40.1256', '57.0'], ['A1YQ52050369051A', '2018-03-19T17:00:00', '116.1493', '40.1269', '100.0'], ['A1YQ520803C70519', '2018-03-19T17:00:00', '116.1302', '40.1291', '54.0'], ['A1YQ520D03C30511', '2018-03-19T17:00:00', '116.1492', '40.1171', '61.0'], ['A1YQ520301DD0514', '2018-03-19T17:00:00', '116.1486', '40.1324', '62.0'], ['A1YQ5207021F0514', '2018-03-19T17:00:00', '116.1403', '40.1199', '35.0']]

value值:

['A1YQ520C02E50517', '2017-05-02T00:00:00', '116.1377', '40.1302', '22.0']
['A1YQ520C02AC0510', '2017-05-02T00:00:00', '116.1406', '40.1375', '17.0']
['A1YQ520201DE0512', '2017-05-02T00:00:00', '116.1318', '40.1373', '22.0']
['A1YQ52090239051E', '2017-05-02T00:00:00', '116.1382', '40.1252', '28.0']
['A1YQ520D0391051A', '2017-05-02T00:00:00', '116.1243', '40.1229', '24.0']
['A1YQ52050369051A', '2017-05-02T00:00:00', '116.1449', '40.119', '33.0']
['A1YQ520803C70519', '2017-05-02T00:00:00', '116.1302', '40.129', '23.0']
['A1YQ520D03C30511', '2017-05-02T00:00:00', '116.1492', '40.117', '35.0']
['A1YQ520301DD0514', '2017-05-02T00:00:00', '116.1486', '40.1324', '31.0']
['A1YQ5207021F0514', '2017-05-02T00:00:00', '116.1403', '40.1199', '23.0']

附程式碼如下:

# -*- coding: utf-8 -*-
"""
@use:解析中加微站歷史小時資料csv格式
"""
import glob
import csv
from xml.dom.minidom import parse 
#import pymssql
from Sqlhelper_excel import SqlHelper
import os

def getExcelData():
    try:
     csvx_list = glob.glob(r'F:\公司專案\2018年專案\中加app\資料\中加資料\校準資料\*.csv')
#     \device\device
#     print(csvx_list)
     print('總共發現%s個csv檔案'% len(csvx_list))
     return csvx_list
    except Exception as e:
     print(e)
     
def open_excel(file):
     try:
         data=csv.reader(open(file,'r'))
         #print(list(data))
         return data
     except Exception as e:
          print(e)


def do():
    #連線資料庫
    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")
    #開啟資料庫連線
    sql=SqlHelper(ip,user,password,database)
    excels=getExcelData()
    for e in excels:
        data=open_excel(e)
        #print(list(data))
        (filepath,tempfilename) = os.path.split(e); 
        (shotname,extension) = os.path.splitext(tempfilename); 
        exceldata= list(data)[1:11]
        try:
            for value in exceldata:
                code= value[0] 
                datestr=value[1]
                pm25=float(value[4])
                print(pm25)
                sqlstr="insert into T_MON_AIRQUALITYHOUR (STATIONCODE, MONIDATE, PM25) values('%s','%s',%f)"  %(code,datestr,pm25)
                sql.InsertOrDelete(sqlstr)
            print(sqlstr)
     
        except Exception as e:
            print(e)
   
     
if(__name__=="__main__"):
    do()