將excel中的內容存入mysql,**如下:
# -*- encoding: utf-8 -*-
import time
import xlrd
import hashlib
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, column, integer, timestamp, char, varchar
# mysql資料庫
engine = create_engine(
'mysql+pymysql://wxx:654321@***/weixx?charset=utf8'
)# engine = create_engine('mysql+pymysql://cuh:123456@***/zk?charset=utf8')
dbsession = sessionmaker(bind=engine)
session = dbsession(
)base = declarative_base(
)class
yonghuigoodsinfoitem
(base)
:"""永輝超市部分商品表"""
__tablename__ =
'yonghui_goods_info'
id= column(integer, primary_key=
true
, autoincrement=
true
) sha = column(char(40)
, unique=
true
) crawled_time = column(timestamp, doc=
) shop_name = column(varchar(
200)
, doc=
) goods_id = column(varchar(
200)
, doc=
) goods_name = column(varchar(
200)
, doc=
) goods_category = column(varchar(
200)
, doc=
) goods_price = column(varchar(
200)
, doc=
) goods_stock = column(varchar(
200)
, doc=
) goods_unit = column(varchar(
200)
, doc=
) goods_image = column(varchar(
200)
, doc=
) goods_desc = column(varchar(
200)
, doc=
) @classmethod
defcreat_table
(cls)
:"""建立資料表"""
cls.__table__.create(bind=engine, checkfirst=
true
)def
insert()
:"""插入資料"""
wb = xlrd.open_workbook(filename=
'yonghui.xls'
)# 開啟檔案
sheet1 = wb.sheet_by_index(0)
# 通過索引獲取**sheet1.nrows
for i in
range(1
, sheet1.nrows)
: rows = sheet1.row_values(i)
# 獲取行內容
shop_name = rows[1]
goods_id = rows[4]
goods_name = rows[5]
goods_category = rows[6]
goods_price = rows[7]
goods_stock = rows[9]
goods_unit = rows[10]
goods_image = rows[11]
goods_desc = rows[12]
time_now = time.strftime(
"%y-%m-%d %h:%m:%s"
) sha = get_hash(goods_id+goods_name)
print
(goods_id, goods_name)
try:
result = session.query(yonghuigoodsinfoitem)
.filter
(yonghuigoodsinfoitem.sha == sha)
.first()if
not result:
item = yonghuigoodsinfoitem(sha=sha,
crawled_time=time_now,
shop_name=shop_name,
goods_id=goods_id,
goods_name=goods_name,
goods_category=goods_category,
goods_price=goods_price,
goods_stock=goods_stock,
goods_unit=goods_unit,
goods_image=goods_image,
goods_desc=goods_desc)
session.add(item)
else
: result.crawled_time = time_now
result.sha = sha
session.commit(
)print
)except baseexception as e:
session.rollback(
) session.close(
)raise baseexception(f"資料插入失敗: "
)def
get_hash
(hash_str)
:hash
= hashlib.sha1(
)hash
.update(
str(hash_str)
.encode(
'utf-8'))
url_sha =
hash
.hexdigest(
)return url_sha
if __name__ ==
'__main__'
: table = yonghuigoodsinfoitem(
) table.creat_table(
) insert(
)
用python操作excel的總結
xlrd 用來讀excel檔案,但是唯讀,不能寫 xlwt 用來寫excel檔案,但是只寫,不能更改已存在的excel中的資料 xlutils 是將xlrd讀入的excel檔案copy乙份,成為xlwt的物件,這樣copy的那乙份就可以寫入了,直接write已經存在的單元格,就是修改,修改完成後儲存...
Python用pandas讀取excel資料
python匯入資料主要用的是read x 方法,x表示匯入檔案的格式 匯入.xlsx檔案,用read excel print 10 用python如何讀取資料源和熟悉資料 10 print 嗶哩嗶哩python課程,主要講用pandas獲取資料源 print 1.匯入資料 n2.熟悉資料 prin...
將excel中的資料匯入hive
步驟二,將該txt檔案匯入linux指定目錄中 步驟三,轉換編碼格式,在指定目錄下執行如下命令 piconv f gb2312 t utf 8 companycode.txt c.txt 步驟四,根據文件中的列,建立表,建表語句如下 hive create table companycode ccn...