用python将从excel中筛选出来的数据一行一行的,自动换行的存入txt
时间: 2024-02-28 14:57:06 浏览: 139
可以使用Python中的pandas库来读取Excel文件并筛选数据,然后将筛选出来的数据一行一行地写入txt文件,并在每一行后自动添加换行符。下面是一个简单的示例代码:
```python
import pandas as pd
# 读取Excel文件
df = pd.read_excel('data.xlsx')
# 筛选数据,假设我们要筛选出“城市”列中为“北京”的数据
filtered_df = df[df['城市'] == '北京']
# 将数据逐行写入txt文件,假设我们要将数据写入“output.txt”文件
with open('output.txt', 'w') as f:
for index, row in filtered_df.iterrows():
# 将每一行数据转换为字符串并写入txt文件,并在末尾添加换行符
f.write(str(row) + '\n')
```
这段代码会将Excel文件中“城市”列中为“北京”的数据逐行写入名为“output.txt”的txt文件中,并在每一行后自动添加换行符。你可以根据自己的需求修改代码中的文件名及筛选条件。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)