Connecting a database with a Pymongo package
Use the XLRD package to read Excel data, and to convert Excel format data to JSON-formatted data due to different data structures
"TypeError: ' str ' object does not a support item assignment" due to a coding problem
To decode data using the Json.loads method
#coding =utf-8import xlrdimport sysimport jsonimport pymongofrom pymongo import mongoclient# Connection Database client=mongoclient (' localhost ', 27017) db=client.scrapyaccount=db.weibodata=xlrd.open_workbook (' test.xlsx ') table=data.sheets () [0]# Read the first line of data in Excel as a field name in MongoDB rowstag=table.row_values (0) nrows=table.nrows#ncols=table.ncols#print Rowsreturndata={}for i in range (1,nrows): #将字段名和excel数据存储为字典形式, and converted to JSON format returndata[i]=json.dumps (dict (Zip (rowstag,table.row_values (i))) #通过编解码还原数据 returndata[i]=json.loads (returndata[i]) # Print returndata[i] account.insert (Returndata[i])
Python implementation adds Excel data to MongoDB