如何从非结构化文本创建python字典?

时间:2018-12-28 20:57:26

标签: python python-2.7 dictionary

我在文本文件中存在一组损坏的链接检查器结果:

Getting links from: https://www.foo.com/
├───OK─── http://www.this.com/
├───OK─── http://www.is.com/
├─BROKEN─ http://www.broken.com/
├───OK─── http://www.set.com/
├───OK─── http://www.one.com/
5 links found. 0 excluded. 1 broken.

Getting links from: https://www.bar.com/
├───OK─── http://www.this.com/
├───OK─── http://www.is.com/
├─BROKEN─ http://www.broken.com/
3 links found. 0 excluded. 1 broken.

Getting links from: https://www.boo.com/
├───OK─── http://www.this.com/
├───OK─── http://www.is.com/
2 links found. 0 excluded. 0 broken.

我正在尝试编写一个脚本,该脚本读取文件中的内容,并创建一个词典列表,其中每个根链接为键,其子级为值(包括摘要行)。

我试图实现的输出如下:

{"Getting links from: https://www.foo.com/": ["├───OK─── http://www.this.com/", "├───OK─── http://www.is.com/", "├─BROKEN─ http://www.broken.com/", "├───OK─── http://www.set.com/", "├───OK─── http://www.one.com/", "5 links found. 0 excluded. 1 broken."], 
"Getting links from: https://www.bar.com/": ["├───OK─── http://www.this.com/", "├───OK─── http://www.is.com/", "├─BROKEN─ http://www.broken.com/", "3 links found. 0 excluded. 1 broken."],
"Getting links from: https://www.boo.com/": ["├───OK─── http://www.this.com/", "├───OK─── http://www.is.com/", "2 links found. 0 excluded. 0 broken."] }

这是我到目前为止所拥有的:

result_list = []

with open('link_checker_result.txt', 'r') as f:
    temp_list = f.readlines()
    for line in temp_list:
        result_list.append(line)

哪个给我输出:

['Getting links from: https://www.foo.com/', '├───OK─── http://www.this.com/', '├───OK─── http://www.is.com/', '├─BROKEN─ http://www.broken.com/', '├───OK─── http://www.set.com/', '├───OK─── http://www.one.com/', '5 links found. 0 excluded. 1 broken.', 'Getting links from: https://www.bar.com/', '├───OK─── http://www.this.com/', '├───OK─── http://www.is.com/', '...'  ]

我认识到这些集合中的每一个都有一些共同的特征,例如,它们之间有一个空白行,或者它们以“ Getting ...”开头。在写字典之前,我应该尝试一下吗?

我是Python的新手,所以我承认我不确定自己是否朝着正确的方向前进。真的很感谢一些专家的关注!预先感谢!

2 个答案:

答案 0 :(得分:2)

这实际上可能很短,在4行代码中:

finalDict = {}
with open('link_checker_result.txt', 'r') as f:
    lines = list(map(lambda line: line.split('\n'),f.read().split('\n\n')))
    finalDict = dict((elem[0],elem[1:]) for elem in lines)
print(finalDict)

输出:

{'Getting links from: https://www.foo.com/': ['+---OK--- http://www.this.com/', '+---OK--- http://www.is.com/', '+-BROKEN- http://www.broken.com/', '+---OK--- http://www.set.com/', '+---OK--- http://www.one.com/'], 'Getting links from: https://www.bar.com/': ['+---OK--- http://www.this.com/', '+---OK--- http://www.is.com/', '+-BROKEN- http://www.broken.com/'], 'Getting links from: https://www.boo.com/': ['+---OK--- http://www.this.com/', '+---OK--- http://www.is.com/']}

以上代码的作用是,读取输入文件并使用两个连续的换行符\n对其进行分割,以获取每个URL的链接。

最后,它会创建第一个元素和每个列表其余部分的元组,并将它们转换为finalDict字典中的键值对。

以下是一种更容易理解的方法:

finalDict = {}
with open('link_checker_result.txt', 'r') as f:
    # Getting data and splitting in order to get each url and its links as a unique list element.
    data = f.read().split('\n\n')
    # Splitting each of the above created elements and discarding the last one which is redundant.
    links = [line.split('\n') for line in data]
    # Transforming these elements into key-value pairs and inserting them in the dictionary.
    finalDict = dict((elem[0],elem[1:]) for elem in links)
print(finalDict)

答案 1 :(得分:0)

这将产生您想要的结果:

result = {}

with open('link_checker_result.txt', 'r') as f:
    temp_list = f.readlines()
    key = ''
    value = []
    for line in temp_list:
        if not line:
            result[key] = value
            key = ''
            value = []
        elif not key:
            key = line
        else:
            value.append(line)

    if key:
      result[key] = value