Created
October 7, 2014 23:09
-
-
Save ianmac45/67e62a6536898703f4e0 to your computer and use it in GitHub Desktop.
example for parsing an excel spreadsheet and loading into a database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/local/bin/python | |
import MySQLdb | |
from openpyxl import load_workbook | |
# Connect to Database | |
database = MySQLdb.connect(host="127.0.0.1", # your host, usually localhost | |
user="", # your username | |
passwd="", # your password | |
db="table") # name of the data base | |
cursor = database.cursor() | |
# Load the Excel Workbook | |
workBook = load_workbook('stats.xlsx') | |
dataSheet = workBook.get_active_sheet() | |
#Query for Database | |
query = """INSERT INTO successes (...) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""" | |
#Counter to Keep Track of current row | |
# row 0 is the header. | |
i = 1 | |
#stringifies the data and nullifies it if necessary | |
def fix(obj): | |
data = str( obj ) | |
if data == 'None': | |
return None | |
return data | |
# Start Iterating through the Sheet and Get the Fields in Each Column | |
while(True): | |
date = fix( dataSheet.cell( row = i, column = 0 ).value ) | |
case_num = fix( dataSheet.cell( row = i, column = 1 ).value ) | |
# if they're both null, we're done ! | |
if date == None and case_num == None: | |
break | |
var = fix( dataSheet.cell( row = i, column = 2 ).value ) | |
var = fix( dataSheet.cell( row = i, column = 3 ).value ) | |
var = fix( dataSheet.cell( row = i, column = 4 ).value ) | |
var = fix( dataSheet.cell( row = i, column = 5 ).value ) | |
var = fix( dataSheet.cell( row = i, column = 6 ).value ) | |
var = fix( dataSheet.cell( row = i, column = 7 ).value ) | |
var = fix( dataSheet.cell( row = i, column = 8 ).value ) | |
var = fix( dataSheet.cell( row = i, column = 9 ).value ) | |
var = fix( dataSheet.cell( row = i, column = 10 ).value ) | |
# Assign the Retrieved Data into a List | |
values = ( var, var, var, var, var, var, | |
var, var, var, var, var ) | |
# Execute the Database Query with the List of Retrieved Data | |
cursor.execute( query, values ) | |
# Success indicator, Update the Row for the Next Iteration | |
print "Entry " + str(i) + " Valid" | |
i += 1 | |
# Commit the Changes | |
print "Database Update" | |
cursor.close() | |
database.commit() | |
database.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment