python - 我正在尝试将 bibles.print("Gen", 1, 1) 输出保存到文本文件中。我该怎么做呢?
问题描述
从免费圣经进口圣经
bibles.print("Gen", 1, 1)
我的目标是输入一段经文并将这节经文保存到一个文本文件中
解决方案
你写的问题没有多大意义,但标题确实如此,所以我会回答这个问题。以下是如何将输出保存bibles.print("Gen", 1, 1)
到文件中:
from freebible import bibles
def save_to_file(book, chapter, verse, fname=None):
if fname is None:
fname = f'{book}_{chapter}_{verse}.txt'
text = bibles.print(book, chapter, verse)
with open(fname, 'w') as f:
# Change to f.write(str(text[0]) + '\n') if you want the Japanese text
f.write(str(text[-1]) + '\n')
if __name__ == '__main__':
save_to_file("Gen", 1, 1)
默认情况下,脚本会将书的文本保存到一个名为book
_ chapter
_ verse
.txt 的文件中,或者如果您不喜欢,您可以指定一个文件名。函数将book
、chapter
和verse
作为输入。您可以修改以使脚本将这些作为命令行参数并将其传递给上面的函数(如果这对您有用)。
示例用法:
(so) Matthews-MacBook-Pro:bible matt$ ls
bible.py
(so) Matthews-MacBook-Pro:bible matt$ python bible.py
[Ge 1:1] 元始に神天地を創造たまへり
[Gen 1:1] In the beginning God created the heavens and the earth.
(so) Matthews-MacBook-Pro:bible matt$ ls
Gen_1_1.txt bible.py
(so) Matthews-MacBook-Pro:bible matt$ cat Gen_1_1.txt
[Gen 1:1] In the beginning God created the heavens and the earth.
HTH。
推荐阅读
- entity-framework - ASP.NET Core EF:如何在没有任何迁移的情况下使用 EF?
- javascript - 无法从控制台获取实时 API 数据并进入 Leaflet 上的数组
- node.js - 这个 err ValidationError: email: Path `email` 是必需的。节点.js
- java - 在 Spring 中非法尝试将非集合映射为单多多多或元素集合
- c++ - 在 C++ 中从 C dll 创建对象
- python - 为什么数据框变成了一个系列?
- visual-studio - VS 2019 中项目之间的 JS/CSS 智能感知
- 3d - 使用 mtl 显示 OBJ 时,QVTKWidget 未在许多面上显示颜色
- c - 如何使用递归函数在 C 中计算矩阵的行列式
- python - 解释这个帕斯卡算法的工作原理