更新文件中的字数统计 :param file_path: 文件路径 :param word_count: 字数统计结果
(file_path, word_count)
| 496 | return len(words) |
| 497 | |
| 498 | def update_word_count(file_path, word_count): |
| 499 | """ |
| 500 | 更新文件中的字数统计 |
| 501 | :param file_path: 文件路径 |
| 502 | :param word_count: 字数统计结果 |
| 503 | """ |
| 504 | print("Updating word count in the file...") |
| 505 | with open(file_path, 'r', encoding='utf-8') as f: |
| 506 | content = f.read() |
| 507 | |
| 508 | # 使用正则表达式替换字数,同时处理有数字和无数字的情况 |
| 509 | updated_content = re.sub(r'全文(\s*?)(\d+)?(\s*?)字', f'全文 {word_count} 字', content) |
| 510 | |
| 511 | with open(file_path, 'w', encoding='utf-8') as f: |
| 512 | f.write(updated_content) |
| 513 | |
| 514 | def copy_to_archive(source_file, pub_date): |
| 515 | """ |