python如何筛选出没有表头的csv文件中大于70小于72的行,并写入另一个csv文件中
时间: 2024-02-15 10:02:26 浏览: 28
可以使用Python内置的csv模块来读取和写入csv文件,并使用条件语句筛选出符合要求的行,具体实现如下:
```python
import csv
# 打开原始csv文件和目标csv文件
with open('input.csv', 'r', newline='') as input_file, open('output.csv', 'w', newline='') as output_file:
# 创建csv读取器和写入器
reader = csv.reader(input_file)
writer = csv.writer(output_file)
# 遍历每一行数据
for row in reader:
# 如果当前行第一列大于70并且小于72,则写入目标csv文件中
if row and float(row[0]) > 70 and float(row[0]) < 72:
writer.writerow(row)
```
需要注意的是,这里假设原始csv文件没有表头,并且第一列为数字类型。如果原始文件有表头,则需要在读取数据时跳过第一行,如下所示:
```python
import csv
# 打开原始csv文件和目标csv文件
with open('input.csv', 'r', newline='') as input_file, open('output.csv', 'w', newline='') as output_file:
# 创建csv读取器和写入器
reader = csv.reader(input_file)
writer = csv.writer(output_file)
# 跳过第一行表头
next(reader)
# 遍历每一行数据
for row in reader:
# 如果当前行第一列大于70并且小于72,则写入目标csv文件中
if row and float(row[0]) > 70 and float(row[0]) < 72:
writer.writerow(row)
```
另外,需要注意文件路径和文件名需要根据实际情况修改。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)