以下是我的标准:
文本文件中的文本如下所示:
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我知道事情搞砸了,所以请你帮我理顺一下。
--我认为我正在做的事情的总结:
从文本文件中创建一个字典,然后以某种方式让用户输入一章和一节,然后让程序能够吐出这些章节并写出诗句。
发布于 2013-08-01 02:06:05
如果我没有弄错的话,行号已经在文件中了。因此,您只需迭代并构建您的dict:
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']https://stackoverflow.com/questions/17983781
复制相似问题