0

ここに私の基準があります:

  1. 聖書全体のテキストのコピーを入手する

  2. これは、開いたり、読んだり、フィールドに分割したりする準備ができているはずです

  3. これを使用して、bible という名前の永続的な辞書変数を作成します

  4. 私が示したように入力すると、プログラムがコマンドラインに入力した内容を認識できるように、Python プログラムをセットアップします。言葉を求めさせないでください。

  5. その参照の意味を解析して、本、章、開始節、および終了節を取得します。その他のバリエーションは、rev 1:1、rev 12、または rev 10:1-3 です。

  6. 印刷時には、新しい行に折り返す前に幅 100 文字の出力制限があり、左側に参照が表示され、いくつかのスペースとテキストが右側に配置されるように十分にインデントする必要があります。

テキスト ファイル内のテキストは次のようになります。

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. 

これまでの私のコードは次のようになります。

import os
import sys
import re

word_search = raw_input(r'Enter a word to search: ')
book = open("kjv.txt", "r")
first_lines = {36: 'Genesis', 4812: 'Exodus', 8867: 'Leviticus', 11749: 'Numbers', 15718: 'Deuteronomy',
           18909: 'Joshua', 21070: 'Judges', 23340: 'Ruth', 23651: 'I Samuel', 26641: 'II Samuel',
           29094: 'I Kings', 31990: 'II Kings', 34706: 'I Chronicles', 37378: 'II Chronicles',
           40502: 'Ezra', 41418: 'Nehemiah', 42710: 'Esther', 43352: 'Job', 45937: 'Psalms', 53537: 'Proverbs',
           56015: 'Ecclesiastes', 56711: 'The Song of Solomon', 57076: 'Isaih', 61550: 'Jeremiah',
           66480: 'Lamentations', 66961: 'Ezekiel', 71548: 'Daniel' }


for ln, line in enumerate(book):
     if word_search in line:
         first_line = max(l for l in first_lines if l < ln)
         bibook = first_lines[first_line]

         template = "\nLine: {0}\nString: {1}\nBook:\n"
         output = template.format(ln, line, bibook)
         print output

私はそれがかなりめちゃくちゃであることを知っているので、私をまっすぐにするのを手伝ってください.

私がやっていると思うことの要約:

テキスト ファイルから辞書を作成し、何らかの方法でユーザーに章と節を入力してもらい、プログラムがそれらの章と節を吐き出せるようにします。

4

3 に答える 3

1

編集:

import csv
import string

reader = csv.reader(open('bible.txt','rb'),delimiter="|")
bible = dict()

for line in reader:
    chapter = line[1].split()[0]
    key = line[1].split()[1]
    linenum = line[0].strip()
    bible[chapter] = bible.get(chapter,dict())
    bible[chapter].update({key:line[2],linenum:line[2]})

entry = raw_input('Entry?')
key = ''
chapter = ""

for char in entry:
    if char in string.letters:
        chapter += char.lower()
    elif char in map(str,range(10)):
        key += char
    else:
        key += ":"

print "Looking in chapter", chapter
print "Looking for", key

try:
    print bible[chapter][key]
except KeyError:
    print "This passage is not in the dictionary."

実行時:

>python bible.py
Entry?:gen1-1
In the beginning God created the heaven and the earth

>python bible.py
Entry?:gen0
In the beginning God created the heaven and the earth

>python bible.py
Entry?:gen1:1
In the beginning God created the heaven and the earth.

数字が数字以外の何かで区切られている限り、入力は正しく機能し、#:#:# に変換されます。chapter-pagenum または chapter-key のいずれかを使用できます

于 2013-08-01T01:41:39.823 に答える
0
Here is what I was trying to get out of this

#!/usr/bin/python

import shelve # we will use the persistent dictionary
import sys

bible = shelve.open("bible.db")
try:
    k = bible['rev 22:21']   # try this, if it fails then build the database
except:
    print "Building..."
    m=open("kjv.txt").read().split("\n")   # this reads the whole file into a flat array with 31102 entries.
    for i in range (len(m)):
        p = m[i].split(" | ")
        if (len(p) == 3):
            nn = int(p[0])
            rf = p[1]
            tx = p[2]
            bible[rf] = tx


book = sys.argv[1]
ch  = sys.argv[2]
# 3 forms
# 12:1-8
# 12
# 12:3
if (ch.find(":") < 0):
    for v in range (1,200):
        p = "%s %d:%d" % (book,ch,v)
        try:
            print p,bible[p]
        except:
            sys.exit(0)


if (ch.find(":") > 0):
    if (ch.find("-") < 0):
        # form 12:3
        (c,v1) = ch.split(":")
        v2 = v1
        r = "%s %s:%s" % (book,c,v1)
        print r,bible[r]

    else:
        (c,v1) = ch.split(":")
        (vs,ve) = v1.split("-")
        for i in range (int(vs),int(ve)+1):
            r = "%s %s:%d" % (book,c,i)
            print r,bible[r]
于 2013-08-07T13:10:37.060 に答える