Skip to content

Commit 0b37b2f

Browse files
Create 2024-10-21-pandas读取excel示例.md
1 parent c299518 commit 0b37b2f

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
layout: post
3+
title: "pandas读取excel示例"
4+
date: 2024-10-21 17:29:20 +0800
5+
categories:
6+
- python
7+
- pandas
8+
tags:
9+
- excel
10+
---
11+
12+
有时候用excel处理数据。
13+
14+
虽然一直在用python写代码,不过好记性确实不如烂笔头,哈哈,备注下。
15+
16+
17+
pandas读写excel
18+
19+
```python
20+
21+
22+
import pandas as pd
23+
import pyperclip
24+
25+
26+
def main():
27+
excel = pd.ExcelFile('C:\\Users\\ray\\Desktop\\sharefiles\\移远咻电10月到期.xlsx')
28+
# 读取第0个sheet, 当然可以直接传sheet表名为参数
29+
df = excel.parse(0, header=1)
30+
# 取第1到2列的所有列为imei
31+
# iloc函数参数, iloc[行起始位置:行结束位置,列起始位置:列结束位置]
32+
imeis = df.iloc[:,1:2]
33+
values = imeis.values
34+
res = "select `sim_code` ,`sn_id` ,`model` from `cp_equipment_metadata` WHERE `sim_code` in ("
35+
for tmp in values :
36+
res =res + "'" + tmp[0].strip() + "',"
37+
pass
38+
39+
res = res[:-1]
40+
res += ")"
41+
pyperclip.copy(res)
42+
print("已经拷贝到剪切板")
43+
44+
45+
pass
46+
if __name__=="__main__":
47+
main()
48+
```

0 commit comments

Comments
 (0)