读到结束文件后,你需要用结束文件里的批号在开始数据库里面检索同批号那一条数据,然后把它捞出来。怎么写
时间: 2023-05-12 11:04:30 浏览: 52
您可以使用以下代码来实现这个功能:
```
import csv
def search_batch(batch_number, start_file, end_file):
with open(start_file, 'r') as start_csv, open(end_file, 'r') as end_csv:
start_reader = csv.reader(start_csv)
end_reader = csv.reader(end_csv)
next(start_reader) # skip header row in start file
next(end_reader) # skip header row in end file
for row in end_reader:
if row[0] == batch_number:
for start_row in start_reader:
if start_row[0] == batch_number:
return start_row
break
return None
```
这个函数需要三个参数:批号、开始文件名和结束文件名。它会打开这两个文件,跳过它们的标题行,然后在结束文件中搜索与给定批号匹配的行。一旦找到匹配的行,它会在开始文件中搜索同一批号的行,并返回第一个匹配的行。如果没有找到匹配的行,它会返回 None。
请注意,这个函数假设开始文件和结束文件都是 CSV 格式,并且它们的第一列是批号。如果您的文件格式不同,您需要相应地修改代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![bat](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)