您如何将该文本文件转换为字典? (蟒蛇)

时间:2020-08-13 03:11:02

标签: python file dictionary object methods

我有一个.txt文件,内容为:

Areca Palm
2018-11-03 18:21:26
Tropical/sub-Tropical plant
Leathery leaves, mid to dark green
Moist and well-draining soil
Semi-shade/full shade light requirements
Water only when top 2 inches of soil is dry
Intolerant to root rot
Propagate by cuttings in water

Canary Date Palm 
2018-11-05 10:12:15
Semi-shade, full sun
Dark green leathery leaves
Like lots of water,but soil cannot be water-logged
Like to be root bound in pot

我想将这些.txt文件转换为python词典,并且输出应如下所示:

d = {'Areca Palm': ('2018-11-03 18:21:26', 'Tropical/sub-Tropical plant', 'Leathery leaves, mid to dark green', 'Moist and well-draining soil'..etc 'Canary Date Palm': ('2018-11-05 10:12:15', 'Semi-shade, full sun'...)

我该怎么做?

2 个答案:

答案 0 :(得分:0)

以下代码显示了一种执行此操作的方法,即使用非常简单的两种状态机读取文件:

with open("data.in") as inFile:
    # Initialise dictionary and simple state machine.

    afterBlank = True
    myDict = {}

    # Process each line in turn.

    for line in inFile.readlines():
        line = line.strip()

        if afterBlank:
            # First non-blank after blank (or at file start) is key
            # (blanks after blanks are ignored).

            if line != "":
                key = line
                myDict[key] = []
                afterBlank = False
        else:
            # Subsequent non-blanks are additional lines for key
            # (blank after non-blank switches state).

            if line != "":
                myDict[key].append(line)
            else:
                afterBlank = True

# Dictionary holds lists, make into tuples if desired.

for key in myDict.keys():
    myDict[key] = tuple(myDict[key])

import pprint
pprint.pprint(myDict)

使用输入数据可以得到输出(与标准Python pprint相比,print的输出更具可读性)

{'Areca Palm': ('2018-11-03 18:21:26',
                'Tropical/sub-Tropical plant',
                'Leathery leaves, mid to dark green',
                'Moist and well-draining soil',
                'Semi-shade/full shade light requirements',
                'Water only when top 2 inches of soil is dry',
                'Intolerant to root rot',
                'Propagate by cuttings in water'),
 'Canary Date Palm': ('2018-11-05 10:12:15',
                      'Semi-shade, full sun',
                      'Dark green leathery leaves',
                      'Like lots of water,but soil cannot be water-logged',
                      'Like to be root bound in pot')}

答案 1 :(得分:0)

通过编写函数可以大大简化许多解析问题 处理文件并一次产生一行有意义的行。 通常,这部分所需的逻辑非常简单。和 它保持简单,因为该功能与其他功能无关 有关较大问题的详细信息。

然后,该步骤简化了下游代码,该代码侧重于解构 一次一个有意义的部分。这部分可以忽略较大的文件问题-同样 保持简单。

说明:

import sys

def get_paragraphs(path):
    par = []
    with open(path) as fh:         # The basic pattern tends to repeat:
        for line in fh:
            line = line.rstrip()
            if line:               # Store lines you want.
                par.append(line)
            elif par:              # Yield prior batch.
                yield par
                par = []
        if par:                    # Don't forget the last one.
            yield par

path = sys.argv[1]
d = {
    p[0] : tuple(p[1:])
    for p in get_paragraphs(path)
}