'''自主创建一个Excel文档(默认为Cust.xlsx),利用SQLite模块和CSV模块完成以下任务。
(1)设计程序把Excel文档中的数据,导入到SQLite数据库Custcsv.db的Custcsv表中
(2)把Custcsv.db中Custcsv表中的数据写入到文本文件Cust.txt'''
import sqlite3
import csv
import xlrd
xlsx
=xlrd
.open_workbook
('Cust.xlsx')
table
=xlsx
.sheet_by_index
(0)
with open('Cust.csv','w',encoding
='utf-8',newline
='') as f
:
write
=csv
.writer
(f
)
for row
in range(table
.nrows
):
row_value
=table
.row_values
(row
)
write
.writerow
(row_value
)
cn
=sqlite3
.connect
('Custcsv.db')
cur
=cn
.cursor
()
sqls
='create table if not exists Custcsv(StuNo text(10),Name text(5),Gender text(3),Phone text(10),WeChat text(10),Score text(10))'
cur
.execute
(sqls
)
sql
='insert into Custcsv values(?,?,?,?,?,?)'
with open('Cust.csv','r',encoding
='utf-8') as csvf
:
rows
=csv
.reader
(csvf
)
cur
.executemany
(sql
,rows
)
print('finish1')
cn
.commit
()
cur
.close
()
cn
.close
()
cn
=sqlite3
.connect
('Custcsv.db')
cur
=cn
.cursor
()
cur
.execute
('select * from Custcsv')
alldata
=cur
.fetchall
()
print(alldata
)
txtf
=open('Cust.txt','w',encoding
='utf-8')
for data
in alldata
:
print(data
,file=txtf
)
print('finish2')
txtf
.close
()
cur
.close
()
cn
.close
()
转载请注明原文地址:https://blackberry.8miu.com/read-3139.html