0

Here are my criteria:

  1. Get a copy of the text of the whole bible

  2. This should be ready to open, read, split up into fields

  3. Use this to create a persistent dictionary variable called bible

  4. Set up your python program so when you type as I have shown, the program will know what you entered on the command line. Do NOT have it prompt you for the words.

  5. Parse that reference meaning get the book, the chapter, the start verse and end verse. Other variations are rev 1:1, rev 12, or rev 10:1-3

  6. When it prints have the output limit to a width of 100 characters wide before wrapping to a new line, which should then be indent enough to have the reference shown on the left and some spaces and the text aligned on the right.

The text in the text file looks like this:

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. 

My code so far looks like this:

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

I know that It is pretty messed up, so please help to straighten me out.

A summary of what I think I am doing:

Create a dictionary from the text file, then somehow get the user to input a chapter and verse, and then have the program be able to spit those chapter and verses out.

4

3 回答 3

1

EDIT:

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."

When executed:

>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.

As long as your numbers are separated by something that's not a number, the input will work correctly and be converted to #:#:#. You can use either chapter-pagenum or 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 回答
0

如果我没记错的话,行号已经在文件中了。所以你可以迭代并建立你的字典:

bible = {}
for line in boo:
    ln, chapter, verse = line.split('|')
    bible[ln] = verse
    bible[chapter] = verse

# how to get the verse from line number or chapter
print bible[2]
print bible['gen 1:3']
于 2013-08-01T02:06:05.717 回答