首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过输入名称、诗句等来对bible.txt进行排序?

如何通过输入名称、诗句等来对bible.txt进行排序?
EN

Stack Overflow用户
提问于 2013-08-01 01:26:34
回答 3查看 2K关注 0票数 0

以下是我的标准:

  1. 要一份整本圣经的文本
  2. 它应该准备好打开、读取、拆分成字段。
  3. 使用它创建一个名为“圣经”的持久字典变量。
  4. 设置您的python程序,以便当您输入我所显示的内容时,程序将知道您在命令行中输入了什么。不要让它提示你说这些话。
  5. 解析这个参考意义,得到书,章节,开始和结束节。其他变体有1:1、12或10:1-3。
  6. 当打印的输出限制在100个字符宽时,然后再包装到新行,则应该缩进,使引用显示在左侧和一些空格上,文本在右边对齐。

文本文件中的文本如下所示:

代码语言:javascript
复制
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. 

到目前为止,我的代码如下:

代码语言:javascript
复制
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

我知道事情搞砸了,所以请你帮我理顺一下。

--我认为我正在做的事情的总结:

从文本文件中创建一个字典,然后以某种方式让用户输入一章和一节,然后让程序能够吐出这些章节并写出诗句。

EN

Stack Overflow用户

发布于 2013-08-01 02:06:05

如果我没有弄错的话,行号已经在文件中了。因此,您只需迭代并构建您的dict:

代码语言:javascript
复制
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']
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17983781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档