1.python read excel file
To read a specified row and column range of an Excel table, you can use a third-party library in Python pandas
. pandas
The library provides powerful data analysis and processing tools, including the ability to read and process Excel files. The following is a sample code that demonstrates how to use pandas
the library to read a specified range of rows and columns in an Excel table:
import pandas as pd
# 读取Excel文件
df = pd.read_excel('path/to/excel_file.xlsx')
# 指定要读取的行和列范围
start_row = 2 # 起始行(索引为0)
end_row = 5 # 结束行
start_col = 'A' # 起始列
end_col = 'C' # 结束列
# 根据行和列范围提取数据
selected_data = df.loc[start_row:end_row, start_col:end_col]
# 打印提取的数据
print(selected_data)
In the above code, you need to 'path/to/excel_file.xlsx'
replace with the actual path of the Excel file you want to read. The code uses pd.read_excel
the function to read the Excel file and store it in an DataFrame
object.
Then start_row
, end_row
specify the range of rows to read by setting and , starting with row index 0. Also, use start_col
and end_col
to specify the range of columns to read, lettering the column labels.
Next, use to extract the data for the specified range of rows and columns from itdf.loc[start_row:end_row, start_col:end_col]
, and store the result in a variable.DataFrame
selected_data
Finally, print(selected_data)
print .
Note that before running the code, make sure pandas
the library is installed. You can install pandas
the library with the following command:
pip install pandas
2. Read csv file and convert to numpy array
To read a specified range of rows and columns from a CSV file, the library is also available pandas
. pandas
Library provides flexible methods to read and process CSV files. Here is a sample code that demonstrates how to use pandas
the library to read a specified range of rows and columns in a CSV file:
2.1 Read csv file
import pandas as pd
# 读取CSV文件
df = pd.read_csv('path/to/csv_file.csv')
# 指定要读取的行和列范围
start_row = 2 # 起始行(索引为0)
end_row = 5 # 结束行
start_col = 0 # 起始列(索引为0)
end_col = 2 # 结束列
# 根据行和列范围提取数据
selected_data = df.iloc[start_row:end_row+1, start_col:end_col+1]
# 打印提取的数据
print(selected_data)
In the above code, you need to 'path/to/csv_file.csv'
replace with the actual path of the CSV file you want to read. The code uses pd.read_csv
the function to read the CSV file and store it in an DataFrame
object.
Then start_row
, end_row
specify the range of rows to read by setting and , starting with row index 0. At the same time, use start_col
and end_col
to specify the column range to be read, and the starting column index is 0.
Next, use to extract the data for the specified range of rows and columns from itdf.iloc[start_row:end_row+1, start_col:end_col+1]
, and store the result in a variable. It should be noted here that and are to include the specified end row and end column.DataFrame
selected_data
end_row+1
end_col+1
Finally, print(selected_data)
print .
Note that before running the code, make sure pandas
the library is installed. You can install pandas
the library with the following command:
pip install pandas
In addition, according to the actual separator of the CSV file, use the parameter read_csv
of the function sep
to specify the separator, for example: pd.read_csv('path/to/csv_file.csv', sep=',')
, if the CSV file uses a comma as the separator. By default, read_csv
functions use commas as separators.
2.2 Convert to numpy array
To pandas
convert the data read by the library into a NumPy array, you can use values
the attribute. Here is the modified example code:
import pandas as pd
import numpy as np
# 读取CSV文件
df = pd.read_csv('path/to/csv_file.csv')
# 指定要读取的行和列范围
start_row = 2 # 起始行(索引为0)
end_row = 5 # 结束行
start_col = 0 # 起始列(索引为0)
end_col = 2 # 结束列
# 根据行和列范围提取数据
selected_data = df.iloc[start_row:end_row+1, start_col:end_col+1]
# 将提取的数据转换为NumPy数组
array_data = selected_data.values
# 打印转换后的NumPy数组
print(array_data)
In the above code, use selected_data.values
converts selected_data
to a NumPy array and stores the result in array_data
a variable. Then, print(array_data)
print the converted NumPy array using .
Make sure you have installed the NumPy library. You can install the NumPy library with the following command:
pip install numpy
In this way, you can pandas
convert the read data into NumPy arrays for subsequent data processing and analysis.
2.3 Examples
terminal print
(nerf) biter@biter:~/dataset/newer_college/transform_format$ python read_excel.py
x y z qx qy qz qw
2 -3.34605 8.24457 0.138331 0.003850 0.003713 -0.605999 0.795447
3 -3.35150 8.24461 0.141933 0.003800 0.003917 -0.605870 0.795545
4 -3.34477 8.24422 0.141572 0.003592 0.003960 -0.606074 0.795390
5 -3.35222 8.24647 0.137599 0.003687 0.003883 -0.605986 0.795458
[[-3.34605e+00 8.24457e+00 1.38331e-01 3.84963e-03 3.71254e-03
-6.05999e-01 7.95447e-01]
[-3.35150e+00 8.24461e+00 1.41933e-01 3.80030e-03 3.91695e-03
-6.05870e-01 7.95545e-01]
[-3.34477e+00 8.24422e+00 1.41572e-01 3.59196e-03 3.95955e-03
-6.06074e-01 7.95390e-01]
[-3.35222e+00 8.24647e+00 1.37599e-01 3.68695e-03 3.88267e-03
-6.05986e-01 7.95458e-01]]