读取图书信息和读者信息,批量生成图书借阅记录并导出借阅记录代码
时间: 2023-12-12 08:05:20 浏览: 143
可以使用Python编程语言来读取图书信息和读者信息,并批量生成图书借阅记录并导出。下面是一个示例代码:
```python
import csv
def generate_borrow_records(books_file, readers_file, borrow_records_file):
# 读取图书信息
books = []
with open(books_file, 'r') as file:
reader = csv.DictReader(file)
books = list(reader)
# 读取读者信息
readers = []
with open(readers_file, 'r') as file:
reader = csv.DictReader(file)
readers = list(reader)
# 生成借阅记录
borrow_records = []
for book in books:
for reader in readers:
borrow_record = {
'Book ID': book['ID'],
'Book Name': book['Name'],
'Reader ID': reader['ID'],
'Reader Name': reader['Name']
}
borrow_records.append(borrow_record)
# 导出借阅记录
fieldnames = ['Book ID', 'Book Name', 'Reader ID', 'Reader Name']
with open(borrow_records_file, 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(borrow_records)
# 示例用法
generate_borrow_records('books.csv', 'readers.csv', 'borrow_records.csv')
```
请注意,上述代码假设图书信息和读者信息分别保存在名为 `books.csv` 和 `readers.csv` 的CSV文件中,借阅记录将被导出到名为 `borrow_records.csv` 的CSV文件中。您需要根据实际情况修改文件路径和字段名称。此外,您还可以根据需要对代码进行进一步的自定义和优化。
阅读全文