2013-07-26 44 views
0

我對python相當陌生,我正在努力學習。我正在編寫一個程序,它將導入一個包含國王詹姆斯聖經的文本文件。用戶必須輸入例如gen 1:1或gen 1:1-10的聖經經文,它將在原始數據輸入時顯示該經文或經文,直到目前爲止,我已將它輸入到程序接收文件和分割的地方數據輸入我不知道蟒蛇的什麼功能,我可以用它來完成這個Python聖經詩歌查找

bible = open("kjv.txt" , "r").readlines() 
for line in bible: 
    x = line.split("|") 
    print "%s, %s, %s" % (x[0], x[1], x[2]) 

的文本文件的外觀

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. 
3 | gen 1:4 | And God saw the light, that it was good: and God divided the light from the darkness. 
4 | gen 1:5 | And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day. 
5 | gen 1:6 | And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters. 
6 | gen 1:7 | And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so. 
7 | gen 1:8 | And God called the firmament Heaven. And the evening and the morning were the second day. 
8 | gen 1:9 | And God said, Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so. 
9 | gen 1:10 | And God called the dry land Earth; and the gathering together of the waters called he Seas: and God saw that it was good. 
+0

「0 |」,「1 |」等是否表示換行? – tehsockz

+4

問題是什麼? –

+0

我會說你應該真的使用數據庫(例如通過[python的sqlite3庫](http://docs.python.org/2/library/sqlite3.html)),因爲聖經是相當大的 - 但我只是從[這裏](http://printkjv.ifbweb.com/)下載它,整個事情(未壓縮)只有4.3MB和約34k行文本,所以即使是逐行讀取整個文件查詢可能不會花費很長時間在任何現代系統上。請記住,處理原始文本文件不會是最有效的方式。 – DaoWen

回答

3
bibletext = """the bible contents""" 

bible = {} 
for line in bibletext.splitlines(): 
    number,bv,contents = line.split(" | ") 
    book,verse = bv.strip().split(" ") 
    print book 
    print bible 
    if book in bible: 
     bible[book].append([verse,contents]) 
    else: 
     bible[book] = [verse,contents] 

print bible 

這將返回一個bible字典的樣本,使用書籍作爲密鑰(所以你可以做bible['gen'],例如,並獲得該書的內容)。一本書的內容保存爲一個列表的列表,像這樣:

[['1:1', 'In the beginning God created the heaven and the earth.', ['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. ']] 

在未來,如果你需要更具體的東西,請指定,在你的問題。

+0

事實證明它的接收AttributeError:'list'對象沒有屬性'splitlines' – user1839040

+0

如果你使用'bibletext = open(「kjv.txt」,「r」).readlines()',那麼移除「 '.splitlines'「,因爲'.readlines()'預先分割了行。 – tehsockz

0

這裏有一些東西,你可以用它來保持與這個推進:

分裂

假設之後,你有白色的空間,以擺脫:

a = " white space on both sides " 
b = a.strip() 
print b # "white space on both sides" 

我建議把你的數據在字典,以便您可以按照章節和經文查找它

bible = {} 
for line in bible: 
    x = line.split("|") 
    bible[x[1].strip()] = x[2].strip() 

chapter_verse = raw_input('Enter a chapter and verse: ') 
print bible[chapter_verse]