In this article, examples for you to share the specific code of python form access for your reference, the details are as follows
xlwt/xlrd libraries Storing Excel files: (if there are characters in the stored data, then there is a small change in the way it is written)
import xlwt workbook = (encoding='utf-8') booksheet = workbook.add_sheet('Sheet 1', cell_overwrite_ok=True) #Store first rows cell(1,1) and cell(1,2) (0,0,34) (0,1,38) #Store second row of cell(2,1) and cell(2,2) (1,0,36) (1,1,39) #Store one line of data rowdata = [43,56] for i in range(len(rowdata)): (2,i,rowdata[i]) ('test_xlwt.xls')
Reading an Excel file: (again, for numeric type data)
import xlrd workbook = xlrd.open_workbook('D:\\Py_exercise\\test_xlwt.xls') print(workbook.sheet_names()) #View all sheets booksheet = workbook.sheet_by_index(0) # Fetch the first sheet with the index booksheet = workbook.sheet_by_name('Sheet 1') # or take a sheet by name #Read cell data cell_11 = booksheet.cell_value(0,0) cell_21 = booksheet.cell_value(1,0) # Read a line of data row_3 = booksheet.row_values(2) print(cell_11, cell_21, row_3) >>>34.0 36.0 [43.0, 56.0]
openpyxl library Stores Excel files:
from openpyxl import Workbook workbook = Workbook() booksheet = # Get the currently active sheet, the default is the first sheet. #Store the first row of cells cell(1,1) (1,1).value = 6 # This method indexes from 1 ("B1").value = 7 #Store one line of data ([11,87]) ("test_openpyxl.xlsx")
Read the Excel file:
from openpyxl import load_workbook workbook = load_workbook('D:\\Py_exercise\\test_openpyxl.xlsx') #booksheet = #Get the currently active sheet, the default is the first sheet. sheets = workbook.get_sheet_names() # Get sheet from name booksheet = workbook.get_sheet_by_name(sheets[0]) rows = columns = # Iterate over all the rows for row in rows: line = [ for col in row] # Reading values through coordinates cell_11 = ('A1').value cell_11 = (row=1, column=1).value
It's really the same in principle, with some differences in the way it's written.
Actually, if you don't need to store it in a format, I think it's fine to save it as a csv file:
import pandas as pd csv_mat = ((0,2),float) csv_mat = (csv_mat, [[43,55]], axis=0) csv_mat = (csv_mat, [[65,67]], axis=0) csv_pd = (csv_mat) csv_pd.to_csv("test_pd.csv", sep=',', header=False, index=False)
Because it's so easy to read:
import pandas as pd filename = "D:\\Py_exercise\\test_pd.csv" csv_data = pd.read_csv(filename, header=None) csv_data = (csv_data, dtype=float)
This is the whole content of this article.