Python table access method, python Table Access
The example in this article shares the python table access code for your reference. The specific content is as follows:
Xlwt/xlrd: (if there are characters in the stored data, there is a slight change in writing)
Import xlwt workbook = xlwt. workbook (encoding = 'utf-8') booksheet = workbook. add_sheet ('sheet 1', cell_overwrite_ OK = True) # store the first row of cell () and cell () booksheet. write (0, 0, 34) booksheet. write (, 38) # store the second row of cell () and cell () booksheet. write (1, 0, 36) booksheet. write (, 39) # store a row of rowdata = [] for I in range (len (rowdata): booksheet. write (2, I, rowdata [I]) workbook.save('test_xlwt.xls ')
Read an Excel file: (For numeric data)
Import xlrd workbook = xlrd. open_workbook ('d: \ Py_exercise \ test_xlwt.xls ') print (workbook. sheet_names () # view all sheet booksheet = workbook. sheet_by_index (0) # Use the index to obtain the first sheet booksheet = workbook. sheet_by_name ('sheet 1') # or use the name to get Sheet # Read cell data cell_11 = booksheet. cell_value (0, 0) cell_21 = booksheet. cell_value () # Read a row 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 saves Excel files:
From openpyxl import Workbook workbook = Workbook () booksheet = workbook. active # obtain the currently active sheet. The default value is the first sheet # store the cell () booksheet of the first row. cell (1, 1 ). value = 6 # This method index starts from 1 booksheet. cell ("B1 "). value = 7 # Save a row of Data booksheet. append ([11, 87]) workbook. save ("test_openpyxl.xlsx ")
Read Excel files:
From openpyxl import load_workbook workbook = load_workbook ('d: \ Py_exercise \ test_openpyxl.xlsx ') # booksheet = workbook. active # obtain the currently active sheet. The default value is the first sheet sheets = workbook. get_sheet_names () # obtain sheet booksheet = workbook from the name. get_sheet_by_name (sheets [0]) rows = booksheet. rows columns = booksheet. columns # iterate all rows for row in rows: line = [col. value for col in row] # Read the value cell_11 = booksheet through coordinates. cell ('a1 '). value cell_11 = booksheet. cell (row = 1, column = 1 ). value
The principle is actually the same, but there are some differences in writing.
In fact, if there is no requirement on the storage format, I think it is quite good to save it as a csv file:
import pandas as pd csv_mat = np.empty((0,2),float) csv_mat = np.append(csv_mat, [[43,55]], axis=0) csv_mat = np.append(csv_mat, [[65,67]], axis=0) csv_pd = pd.DataFrame(csv_mat) csv_pd.to_csv("test_pd.csv", sep=',', header=False, index=False)
Because it is very simple to read:
import pandas as pd filename = "D:\\Py_exercise\\test_pd.csv" csv_data = pd.read_csv(filename, header=None) csv_data = np.array(csv_data, dtype=float)
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.