這是我的困境:我正在用Python編寫一個應用程序,它將允許我搜索平面文件(KJV bible.txt )爲特定的字符串,並返回搜索的行號,書籍和字符串。但是,我還想返回找到該字符串的章節和詩句。這就要求我走到這條線的開頭,並獲得章節和詩句的編號。我是一名Python初學者,目前我仍在閱讀Guido van Rossum的Python教程。這是我想爲聖經學習小組完成的事情;幾乎可以在任何地方運行cmd模塊的東西。我感謝任何幫助......謝謝。下面是從聖經章節的示例的摘錄:我如何從一行文本的開頭獲取數字,將它們分開並將它們打印出來
Daniel
1:1 In the third year of the reign of Jehoiakim king of Judah came
Nebuchadnezzar king of Babylon unto Jerusalem, and besieged it.
說我搜索「雅敬」和搜索結果中的一個是上面的第一道防線。我想去這行前面的數字(在這種情況下是1:1),並獲得章節(1)和詩歌(1)並將它們打印到屏幕上。
1:2 And the Lord gave Jehoiakim king of Judah into his hand, with part
of the vessels of the house of God: which he carried into the land of
Shinar to the house of his god; and he brought the vessels into the
treasure house of his god.
代碼:
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
如果你希望你的應用程序是可移植的,那麼你可能想從程序中創建一個可執行文件,這樣就不必安裝python來運行它。看看這個[鏈接](http://www.py2exe.org/index.cgi/Tutorial)一個簡單的方法來完成這個。 –
@SC鬼 - 謝謝...是的,目前我使用py2exe。 – suffa