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