简体   繁体   中英

How can I create a nested dictionary from a regex in python?

I've created a regular expression to search through a block of text and pull out two things. My goal is to create a CSV (or Excel file) with the following information:

  1. A set of 4-digit codes
  2. The matching set of spell effects.

I created my regex expression, which I've tested and which successfully pulls out all of the relevant information that I need into a giant block of text. I initially tried to just iterate through this to create one dictionary, which looked like the following:

WildMagic = {0000: "Target's head turns into a watermelon", 0001: "Target's head turns into a cucumber",...Number: "Effect Text"}

However, when I wrote this to a CSV I got two long rows, one with all of the 4 digit codes and another with all of the effects. I could fix it in Excel, but that's not really the goal of this excercise. After some reading, I thought it might be best to create a dictionary of dictionaries so that when creating the CSV I can use the number and effect labels to create headers for my CSV.

My current code looks like this:

    WildMagic = {}
for effect in textRegex.findall(str(text.read())):
    WildMagic[effect]["item"]=effect[1]
    WildMagic[effect]["value"]=effect[3]

As I sketched out the code, my thinking was that this code would create the dictionary, then iterate through each item in the text block. For each number and effect, it would create a dictionary entry that would look like this:

{1:{"item":NUMBERS, "value":SPELL_EFFECT}, 2:{"item":NUMBERS, "value":SPELL_EFFECT}...}

this loop is capable of printing each of the number, effect pairs, so I know I'm missing something. Any help is appreciated. Thanks!

Edit: Code used to write to CSV below:

with open("WildMagicCSV.csv", "w") as CSVfile:
    file = csv.DictWriter(CSVfile,WildMagic.keys())
    file.writeheader()
    file.writerow(WildMagic)

Edit 2: The text that is being read into the dictionary looks like the following. an example of it is also represented in the initial dictionary that I described above. The input looks like the following:

2642 Huge volumes of ectoplasm ooze from the caster's nostrils
2643 Icy winds buffet the caster for 2d8-1 days
2644 If alive, caster is totally healed in each of the next 1d6 hours

There are 10,000 entries.

First at all, you should consider 0000 as string if you want to keep it 4-digts. I will assume all numbers are string -> '0000'

From WildMagic :

WildMagic = {'0000': "Target's head turns into a watermelon", '0001': "Target's head turns into a cucumber"}
wild_magic = [{'id': k, 'text': v} for k, v in WildMagic.items()]
# output
[{'text': "Target's head turns into a watermelon", 'id': '0000'}, {'text': "Target's head turns into a cucumber", 'id': '0001'}]

Once you're there, you can useDictWriter from csv library:

import csv

with open('result_file.csv', 'w') as _f:
    fieldnames = wild_magic[0].keys()
    writer = csv.DictWriter(_f, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerows(wild_magic)

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