首页 > 解决方案 > 我正在尝试将 bibles.print("Gen", 1, 1) 输出保存到文本文件中。我该怎么做呢?

问题描述

从免费圣经进口圣经

bibles.print("Gen", 1, 1)

我的目标是输入一段经文并将这节经文保存到一个文本文件中

标签: python

解决方案


你写的问题没有多大意义,但标题确实如此,所以我会回答这个问题。以下是如何将输出保存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 的文件中,或者如果您不喜欢,您可以指定一个文件名。函数将bookchapterverse作为输入。您可以修改以使脚本将这些作为命令行参数并将其传递给上面的函数(如果这对您有用)。

示例用法:

(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。


推荐阅读