python - SyntaxError on function definition -
i'm trying pull data excel spreadsheet mysql. script can't find path excel file, , ide (spyder) giving error on line:
def read_excel(r'c:\\users\\parasystems limited\\desktop\\main.xlsx'): invalid syntax
import openpyxl import pymysql mdb def read_excel(r'c:\\users\\parasystems limited\\desktop\\main.xlsx'): masterdict = {} wb = openpyxl.load_workbook('main.xlsx') sheet in wb: arow in range(2, sheet.max_row+1): if sheet['a'+str(arow)].value: masterdict[sheet['a'+str(arow)].value] = { 'equipment number':sheet['b'+str(arow)].value, 'number':sheet['c'+str(arow)].value, 'description':sheet['d'+str(arow)].value, 'manufacturer':sheet['e'+str(arow)].value, 'serial number':sheet['f'+str(arow)].value, 'country of manufacturer':sheet['g'+str(arow)].value, 'functional location description':sheet['h'+str(arow)].value, 'functional location number (short)':sheet['i'+str(arow)].value, 'functional location number':sheet['j'+str(arow)].value, 'cost of servicing , maintenance':sheet['k'+str(arow)].value, 'office location':sheet['l'+str(arow)].value } return masterdict def inputintomysql(masterdict): con = mdb.connect(host= '127.0.0.1', user = 'root', password =none,db='scraping') cur = con.cursor() con: cur.execute("drop table if exists main") cur.execute("create table main (rid int primary key, equipmentnumber varchar(75), description varchar(75),\ manufacturer varchar(50), serialnumber int,countryofmanufacturer varchar(25), \ functionallocationdescription varchar(50), functionallocationnumbershort varchar(75), functionallocationnumber varchar(25),\ costofservicingandmaintenance decimal(15,2),officelocation varchar(35))") in masterdict: cur.execute('insert distributors_nestle(rid, equipmentnumber,description,manufacturer,serialnumber,\ countryofmanufacturer,functionallocationdescription, functionallocationnumbershort,functionallocationnumber\ costofservicingandmaintenance,officelocation) values("%s", "%s", "%s","%s","%s","%s","%s","%s","%s","%s","%s")' %(i,masterdict[i]['equipment number'],masterdict[i]['description'], masterdict[i]['manufacturer'],masterdict[i]['serial number'],masterdict[i]['country of manufacturer'], masterdict[i]['functional location description'], masterdict[i]['functional location number (short)'], masterdict[i]['functional location number'], masterdict[i]['cost of servicing , maintenance'], masterdict[i]['office location'])) con.commit() con.close()
this not valid function definition:
def read_excel(r'c:\\users\\parasystems limited\\desktop\\main.xlsx'): masterdict = {} wb = openpyxl.load_workbook('main.xlsx') ...
you don't have named parameters inside parentheses, raw string.
it looks meant like:
def read_excel(fname=r'c:\users\parasystems limited\desktop\main.xlsx'): masterdict = {} # [unchanged] wb = openpyxl.load_workbook(fname) # _uses_ parameter. ...
also, since using raw string (r'...'
), shouldn't need double backslashes. single backslashes should work. (you'll have verify yourself. don't have access windows system, can't test this.)
Comments
Post a Comment