xlrd库读取workbook中的数据,并将数据存为csv
时间: 2024-05-07 12:15:22 浏览: 25
文件的过程如下:
1. 导入xlrd库和csv库
```
import xlrd
import csv
```
2. 打开要读取的workbook
```
workbook = xlrd.open_workbook('example.xlsx')
```
3. 获取要读取的sheet
```
sheet = workbook.sheet_by_index(0) # 读取第一个sheet
```
4. 读取数据并存储为csv文件
```
with open('example.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for rownum in range(sheet.nrows):
writer.writerow(sheet.row_values(rownum))
```
完整代码如下:
```
import xlrd
import csv
workbook = xlrd.open_workbook('example.xlsx')
sheet = workbook.sheet_by_index(0)
with open('example.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for rownum in range(sheet.nrows):
writer.writerow(sheet.row_values(rownum))
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)