“This is the fourth day of my participation in the Gwen Challenge.

The Library is used to read data from and write data to a variety of data files. This paper takes Excel workbook and CSV file as an example to explain how to read and write files.

1. File reading

The following code is used to read data from an Excel workbook.

import pandas as pd
data = pd.read_excel('data.xlsx')
Copy the code

The Excel workbook file extension here is.xlsx, or.xls for Excel workbook files of 2003 or earlier.

The read_excel() function can set more parameters, as shown below.

data = pd.read_excel('data.xlsx',sheet_name=0,encoding='utf-8')
Copy the code

Sheet_name specifies the worksheet to read. It can be either a sheet name or a number (default is 0, the first sheet). Encoding Specifies the file encoding mode. It is usually set to UTF-8 or GBK to avoid garbled Characters. Index_col Sets the row index of a column.

In addition to reading Excel workbooks, pandas can also read CSV files. CSV files are text files in nature. They only store data. Unlike Excel workbooks, CSV files do not store formats, formulas, macros, and other information. CSV files typically use commas to separate a series of values and can be opened in Excel or a text editor such as Notepad.

The following code is used to read the CSV file.

data = pd.read_csv(‘data.csv’,delimiter=’,’,encoding=’utf-8′)

The delimiter parameter specifies the delimiter symbol in the CSV file. The default value is comma. Encoding Specifies the encoding mode. It is usually set to UTF-8 or GBK to avoid Chinese garbled characters. In addition, the read_csv() function can also set indexed columns with the index_col parameter.

2. File writing

The following code writes data to an Excel workbook.

Data = pd. DataFrame ([[1, 2], [3, 4], [5, 6]] columns = [' column A ', 'B') data. To_excel (' data_new. XLSX)Copy the code

The file storage path here uses relative path, can be written as an absolute path. This will generate a “data_new.xlsx” file in the same folder as the code file.

File relative path and absolute path

Relative paths

The relative path is the folder where the code files are located. For example, data.to_excel (‘data.xlsx’) generates an Excel workbook in the same folder as the code files.

An absolute path

The absolute path is the full path of the file.