![](/img/trans.png)
[英]python sqlite3 uploading data to DB from excel(xls/xlsx)
[英]Filling Tables with data from XLSX files Python3 sqlite3 openpyxl
我正在尋找一種更有效的方法來填充表,而不是使用[用於xx范圍內的xx]並向表中添加新行。 電子表格有數千行,因此需要很長時間。
import openpyxl
import sqlite3
wb=openpyxl.load_workbook('GlobalLandTemperaturesByCountry.xlsx')
print(wb.get_sheet_names())
#GlobalLandTemperaturesByCountry
sheet = wb.get_sheet_by_name('GlobalLandTemperaturesByCountry')
print(sheet.max_row)
#577463 rows
wb2=openpyxl.load_workbook('GlobalLandTemperaturesByState.xlsx')
print(wb2.get_sheet_names())
#GlobalLandTemperaturesByState
sheet2 = wb2.get_sheet_by_name('GlobalLandTemperaturesByState')
print(sheet2.max_row)
#645676 rows
wb3=openpyxl.load_workbook('GlobalLandTemperaturesByMajorCity.xlsx')
print(wb3.get_sheet_names())
#GlobalLandTemperaturesByMajorCi
sheet3 = wb3.get_sheet_by_name('GlobalLandTemperaturesByMajorCi')
print(sheet3.max_row)
#239178 rows
connection = sqlite3.connect('temperature.db')
cursor = connection.cursor()
cursor.execute('''CREATE TABLE Countries
(Date DATE, AverageTemperature REAL, AverageTemperatureUncertainty REAL, Country TEXT)
''')
cursor.execute('''CREATE TABLE States
(Date DATE, AverageTemperature REAL, AverageTemperatureUncertainty REAL, State TEXT, Country TEXT)
''')
cursor.execute('''CREATE TABLE Towns
(Date DATE, AverageTemperature REAL, AverageTemperatureUncertainty REAL, City TEXT, Country TEXT, Latitude REAL, Longitude REAL)
''')
謝謝〜python noobie〜
我決定將xlsx文件轉換為csv文件似乎更快
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.