In [28]:
#파일이름 입력
txt = "asv"
#txt파일의 창세기 제목 표시 형태를 표기.
genesis = "Gen."
with open(txt + ".txt") as file:
lines = []
for line in file:
# The rstrip method gets rid of the "\n" at the end of each line
lines.append(line.rstrip().split(" "))
In [2]:
lastbook = ''
currentchp = ''
jsonstr = '['
for verse in lines:
i = len(verse)
if i < 2: continue
verse[2:] = [' '.join(map(str,verse[2:]))]
currentbook = verse[0]
if currentbook != lastbook and currentbook != genesis:
jsonstr += "\t\t}\n\t}]\n},"
if currentbook != lastbook:
jsonstr += "\n{\n"
jsonstr += "\t\"book\":" + "\"" + currentbook + "\"" + ",\n" + "\t\"chapters\":[{\n"
chpvrs = verse[1].split(":")
if chpvrs[0] != currentchp and chpvrs[0] != "1":
jsonstr += "\n\t\t\t}\n\t\t},\n\t\t{"
if chpvrs[0] != currentchp or currentbook != lastbook:
jsonstr += "\t\t\"" + chpvrs[0] + "\": {\n"
if int(chpvrs[1]) > 1:
jsonstr += ", \n"
jsonstr += "\t\t\t\"" + chpvrs[1] + "\"" + ":" + "\"" + verse[2] + "\""
lastbook = verse[0]
currentchp = chpvrs[0]
jsonstr += "\n\t\t}\n\t}]\n}\n]"
f = open(txt + ".json", 'w')
f.write(jsonstr)
f.close()
In [29]:
import re
lastbook = ''
currentchp = ''
jsonstr = '['
for verse in lines:
i = len(verse)
if i < 2: continue
verse[1:] = [' '.join(map(str,verse[1:]))]
#파일 내용중에 . 으로 책/장절이 구분되지 않은 항목이 있어서 숫자, 문자로 배열 구분함.
bookchpvrs = re.split('(\d+)',verse[0])
currentbook = bookchpvrs[0]
if currentbook != lastbook and currentbook != genesis:
jsonstr += "\t\t}\n\t}]\n},"
if currentbook != lastbook:
jsonstr += "\n{\n"
jsonstr += "\t\"book\":" + "\"" + currentbook + "\"" + ",\n" + "\t\"chapters\":[{\n"
chpvrs[0] = bookchpvrs[1]
chpvrs[1] = bookchpvrs[3]
if chpvrs[0] != currentchp and chpvrs[0] != "1":
jsonstr += "\n\t\t\t}\n\t\t},\n\t\t{"
if chpvrs[0] != currentchp or currentbook != lastbook:
jsonstr += "\t\t\"" + chpvrs[0] + "\": {\n"
if int(chpvrs[1]) > 1:
jsonstr += ", \n"
jsonstr += "\t\t\t\"" + chpvrs[1] + "\"" + ":" + "\"" + verse[1] + "\""
lastbook = bookchpvrs[0]
currentchp = chpvrs[0]
jsonstr += "\n\t\t}\n\t}]\n}\n]"
f = open(txt + ".json", 'w')
f.write(jsonstr)
f.close()
In [ ]: