我想解析一个文件。我有一个文件名称kjv.tsv。在这个文件里面,每一行包括书名,章节名称,诗句编号和诗句。建立字典从逗号分隔变量行
输出应该是这样的:
(ge, 0, 0, In the beginning God created the heaven and the earth.)
(ge, 0, 1, And the earth was .... upon the face of the waters.)
(ge, 0, 2, And God said, Let there be light: and there was light.)
这是我走到这一步。我的函数名是我定义parse_line行:
def parse_line(line):
'''
Converts a line from kjv.tsv into a list of verse information. I.e.
[book name, chapter number, verse number, verse text]
Return a list of verse information
'''
bibletext = open("kjv.tsv" , "r").readlines()
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)
这不行吗?你怎么知道的?你期望它做什么?了解如何创建[mcve]。 –
欢迎来到StackOverflow。请阅读并遵守帮助文档中的发布准则。 [最小,完整,可验证的示例](http://stackoverflow.com/help/mcve)适用于此处。在您发布代码并准确描述问题之前,我们无法有效帮助您。 请注意,您有缩进错误并且没有示例数据文件。 – Prune
代码不起作用。它不会以pkython的形式返回正确的信息。我证明了我正在寻找的例子。 –