简体   繁体   中英

How do you convert this textfile into dictionary? (PYTHON)

I have a.txt file that reads:

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

I want to convert these.txt file into dictionary on python and the output should look something like this:

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'...)

How do I go about doing this?

The following code shows one way to do this, by reading the file with a very simple two-state state machine:

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)

Using your input data gives the output (output with pprint to make a little more readable than the standard Python 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')}

Many parsing problems are greatly simplified by writing a function to process the file and yield its lines one meaningful section at a time. Quite often, the logic needed for this part is pretty simple. And it stays simple, because the function isn't concerned with any other details about your larger problem.

That step then simplifies the downstream code, which focuses on deconstruction of one meaningful section at a time. This part can ignore larger file concerns -- also keeping things simple.

An illustration:

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM