1

私はPythonにかなり慣れていないので、学ぼうとしています。ジェームズ王の聖書を含むテキスト ファイルをインポートするプログラムを作成しています。ユーザーは、たとえばgen 1:1またはgen 1:1-10などの聖書の一節を入力する必要があり、プログラムがファイルを受信して​​分割する場所にそれを持っているので、生データ入力時にその節または節が表示されますデータ入力これを完了するために使用できるPythonの機能がわかりません

bible = open("kjv.txt" , "r").readlines()
 for line in bible:
  x = line.split("|")
  print "%s, %s, %s" % (x[0], x[1], x[2])

テキスト ファイルの外観のサンプル

0 | gen 1:1 | In the beginning God created the heaven and the earth. 
1 | gen 1:2 | And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters. 
2 | gen 1:3 | And God said, Let there be light: and there was light. 
3 | gen 1:4 | And God saw the light, that it was good: and God divided the light from the darkness. 
4 | gen 1:5 | And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day. 
5 | gen 1:6 | And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters. 
6 | gen 1:7 | And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so. 
7 | gen 1:8 | And God called the firmament Heaven. And the evening and the morning were the second day. 
8 | gen 1:9 | And God said, Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so. 
9 | gen 1:10 | And God called the dry land Earth; and the gathering together of the waters called he Seas: and God saw that it was good. 
4

2 に答える 2

6
bibletext = """the bible contents"""

bible = {}
for line in bibletext.splitlines():
    number,bv,contents = line.split(" | ")
    book,verse = bv.strip().split(" ")
    print book
    print bible
    if book in bible:
        bible[book].append([verse,contents])
    else:
        bible[book] = [verse,contents]

print bible

bibleこれは、本をキーとして使用して辞書を返します (したがってbible['gen']、たとえば、その本の内容を取得できます)。本の内容は、次のようにリストのリストとして保存されます。

[['1:1', 'In the beginning God created the heaven and the earth.', ['1:2', 'And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters. ']]

今後、より具体的なことが必要な場合は、質問にその旨を明記してください。

于 2013-07-26T05:51:58.813 に答える
1

これを推進し続けるために使用できるいくつかのことを次に示します。

分割後に、取り除く空白があるとします。

a = " white space on both sides "
b = a.strip()
print b  # "white space on both sides"

章ごと、節ごとに調べることができるように、データを辞書に入れることをお勧めします。

bible = {}
for line in bible:
    x = line.split("|")
    bible[x[1].strip()] = x[2].strip()

chapter_verse = raw_input('Enter a chapter and verse: ')
print bible[chapter_verse]
于 2013-07-26T06:02:15.793 に答える