Python table access method, python Table Access

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.