1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Problem with Extracting names of all Pokemon in box-like txt file format

Discussão em 'Python' iniciado por Stack, Setembro 10, 2024.

  1. Stack

    Stack Membro Participativo

    I am looking to parse through a txt file and extract specifically the names of Pokemon as well as their moves and percentages used, in Python, while passing through everything else. A snippet of one pokemon in my txt file is below for reference:

    +----------------------------------------+
    | Great Tusk |
    +----------------------------------------+
    | Raw count: 588311 |
    | Avg. weight: 1.0 |
    | Viability Ceiling: 91 |
    +----------------------------------------+
    | Abilities |
    | Protosynthesis 100.000% |
    +----------------------------------------+
    | Items |
    | Heavy-Duty Boots 27.301% |
    | Booster Energy 22.192% |
    | Leftovers 21.462% |
    | Rocky Helmet 15.298% |
    | Assault Vest 6.317% |
    | Choice Scarf 1.472% |
    | Eject Pack 1.340% |
    | Other 4.618% |
    +----------------------------------------+
    | Spreads |
    | Jolly:0/252/0/0/4/252 21.864% |
    | Jolly:0/252/4/0/0/252 16.285% |
    | Jolly:252/4/0/0/0/252 13.141% |
    | Jolly:4/252/0/0/0/252 5.399% |
    | Impish:252/0/220/0/0/36 4.399% |
    | Adamant:252/252/0/0/4/0 2.986% |
    | Other 35.926% |

    | Moves |
    | Rapid Spin 93.632% |
    | Ice Spinner 74.448% |
    | Headlong Rush 69.895% |
    | Knock Off 31.582% |
    | Stealth Rock 30.997% |
    | Close Combat 30.400% |
    | Earthquake 27.250% |
    | Bulk Up 22.838% |
    | Other 18.958% |
    +----------------------------------------+
    | Tera Types |
    | Ground 23.792% |
    | Steel 21.370% |
    | Water 11.246% |
    | Fire 9.289% |
    | Poison 8.310% |
    | Ice 8.140% |
    | Ghost 5.585% |
    | Fairy 2.820% |
    | Fighting 2.774% |
    | Grass 2.135% |
    | Other 4.538% |
    +----------------------------------------+
    | Teammates |
    | Kingambit 21.019% |
    | Slowking-Galar 20.331% |
    | Gholdengo 19.520% |
    | Dragapult 18.160% |
    | Ogerpon-Wellspring 15.821% |
    | Raging Bolt 14.357% |
    | Iron Valiant 11.794% |
    | Iron Crown 11.787% |
    | Dragonite 11.375% |
    | Roaring Moon 11.083% |
    | Iron Moth 11.021% |
    +----------------------------------------+
    | Checks and Counters |
    | Sinistcha 81.015 (82.43±0.35) |
    | (18.3% KOed / 64.2% switched out)|
    | Serperior 79.509 (81.71±0.55) |
    | (39.9% KOed / 41.8% switched out)|
    | Iron Valiant 78.810 (79.90±0.27) |
    | (40.2% KOed / 39.7% switched out)|
    | Manaphy 78.606 (82.14±0.88) |
    | (42.7% KOed / 39.4% switched out)|
    | Walking Wake 78.526 (79.89±0.34) |
    | (38.7% KOed / 41.2% switched out)|
    | Comfey 77.023 (81.55±1.13) |
    | (36.1% KOed / 45.5% switched out)|
    | Yanmega 76.370 (82.58±1.55) |
    | (51.8% KOed / 30.8% switched out)|
    | Iron Jugulis 76.151 (82.32±1.54) |
    | (49.1% KOed / 33.2% switched out)|
    | Latias 76.106 (79.93±0.96) |
    | (38.9% KOed / 41.0% switched out)|
    | Ogerpon-Wellspring 75.492 (76.55±0.26) |
    | (32.4% KOed / 44.2% switched out)|
    | Enamorus 75.128 (76.42±0.32) |
    | (34.7% KOed / 41.8% switched out)|
    | Cresselia 74.686 (78.51±0.96) |
    | (19.2% KOed / 59.3% switched out)|
    +----------------------------------------+


    Here is my current algorithm for extracting the txt data to an output file. Right now, it outputs the moves of the respective pokemon and "Moves" above them, but not the names of the Pokemon I am getting the moves from.

    with open('text condensed.txt', 'r') as move_text:
    with open('output.txt', 'w') as out:
    should_write = False
    for line in move_text:
    if line.startswith('+'):
    next_line = move_text.readline().strip()

    if next_line and not next_line.startswith("|"):
    out.write(next_line + '\n')
    print(next_line)
    elif 'Raw count: ' in line:
    should_write = False
    elif 'Moves' in line:
    should_write = True
    elif 'Other' in line:
    should_write = False

    if should_write:
    out.write(line)


    When I run the program, my desired output should look something like this:

    Great Tusk
    Moves
    Rapid Spin 93.632%
    Ice Spinner 74.448%
    Headlong Rush 69.895%
    Knock Off 31.582%
    Stealth Rock 30.997%
    Close Combat 30.400%
    Earthquake 27.250%
    Bulk Up 22.838%

    Kingambit
    Moves
    Sucker Punch 97.709%
    ...


    Instead my actual output looks like this:

    | Moves |
    | Rapid Spin 93.632% |
    | Ice Spinner 74.448% |
    | Headlong Rush 69.895% |
    | Knock Off 31.582% |
    | Stealth Rock 30.997% |
    | Close Combat 30.400% |
    | Earthquake 27.250% |
    | Bulk Up 22.838% |
    | Moves |
    | Sucker Punch 97.709% |
    | Swords Dance 90.998% |
    ...

    Continue reading...

Compartilhe esta Página