win10系统,python3.8.0,需要安装pandas和openpyxl
python3.8.0可在官网下载,安装后自带pip,可将pip源配置为国内的pip源
在路径C:\Users\winpc\下新建名称为pip的文件夹
在路径C:\Users\winpc\pip\下新建名称为pip.ini的文件
pip.ini的内容为pip源配置,列如:
[global]
index-url=http://mirrors.aliyun.com/pypi/simple
trusted-host=mirrors.aliyun.com
安装pandas和openpyxl
pip install openpyxl
pip install pandas
要读取的excel内容示例如下
stu_uid |
name |
age |
asdert2 |
zhnagsan |
20 |
qwqety3 |
lisi |
24 |
cvbtyu6 |
wanger |
28 |
文件路径为:D:\project_dev\pandas_tes\text.xlsx
简单python代码如下
#!/usr/bin/env python
# _*_ encoding:utf-8 _*_
import pandas as pd
def read_write_excel():
file_path = "D:\\project_dev\\pandas_tes\\test.xlsx"
data = pd.read_excel(file_path)
#print(data.values)
data_lists = []
for item in data.values:
item_js = {"stduid": item[0],
"stu_name": item[1],
"stu_age": item[2]}
data_lists.append(item_js)
print(data_lists)
df = pd.DataFrame(data_lists)
result_path = "D:\\project_dev\\pandas_tes\\result.xlsx"
df.to_excel(result_path)
if __name__ == '__main__':
read_write_excel()
执行后结果如下
|
stduid |
stu_name |
stu_age |
0 |
asdert2 |
zhnagsan |
20 |
1 |
qwqety3 |
lisi |
24 |
2 |
cvbtyu6 |
wanger |
28 |