Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7811353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:07:53+00:00 2026-06-02T04:07:53+00:00

I need to process the text to create a dictionary {name: quantity} Variants of

  • 0

I need to process the text to create a dictionary {name: quantity}

Variants of text:

2 Cardname
3 Cardname Two
1 Cardname Three

Cardname
Cardname Two
Cardname Three

So i wrote a basic code:

card_list = card_area.splitlines()
card_dict = {}

for card in card_list:
    qty_re = re.search('^\d{1,6}', card)
        if qty_re:
            qty = qty_re.group()
        else:
            qty = 1

     name_re = re.search('[A-Za-z ]+$', card)
        if name_re:
            name = name_re.group()
        else:
            name = None

     if name:
         card_dict[name] = qty

The first question: Can I use the groupdict method if some elements of strings isn’t exists (no qty or empty string).

Second: I also want to consider such formats:

2 x Cardname
3x Cardname Two
1 xCardname Three
1xCardname Four

What is the best way ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:07:55+00:00Added an answer on June 2, 2026 at 4:07 am

    A solution. Notes to follow.

    from collections import defaultdict
    import re
    
    # card_list = card_area.splitlines()
    card_list = [
        "2 Cardname", "3 Cardname Two", "1 Cardname Three",
        "Cardname", "Cardname Two", "Cardname Three",
        "1x Cardname", "4X Cardname Two", "2 X Cardname Three",
    ]
    
    card_dict = defaultdict(int)
    
    pat = re.compile(r'(\d*)\s*(?:[xX]\s+)?(\S.*)')
    
    for card in card_list:
        m = re.search(pat, card)
        if not m:
            continue
        if m.group(1):
            qty = int(m.group(1))
        else:
            qty = 1
    
        name = m.group(2)
        card_dict[name] += qty
    
    
    if not card_dict:
        print("empty card_dict!")
    else:
        for name in sorted(card_dict):
            print("%20s|%4d" % (name, card_dict[name]))
    

    Notes:

    • I recommend pre-compiling the regular expression pattern, for speed.

    • The best way to handle this is a single regular expression pattern that grabs both the count and the card. I have added an optional pattern that recognizes card formats with the optional ‘x’; using a character class I made it match either upper- or lower-case ‘x’. The white space between the number and the ‘x’ is optional but there must be white space between the ‘x’ and the card name, or else the ‘x’ will be treated as part of the card name.

    • If you are not familiar with regular expressions, here is how to read this one: form a match group that matches zero or more digits. This is followed by zero or more white space characters. This is followed by another group, but this following group is flagged with (?: rather than just ( so it is a group but will not make a match group in the output; this group is a character class matching ‘x’ or ‘X’ followed by one or more white space characters. Form another match group, which starts with one non-whitespace character and is followed by zero or more of any character.

    • I believe you want to sum all the cards of the same name? The best for that is to use defaultdict() as I showed here.

    • If no legal card name ever starts with ‘x’ or ‘X’, you could change the pattern to not keep the ‘x’ even when there is no space between it and the card name. To do that, change the pattern to match the ‘x’ from this: (?:[xX]\s+)? to this: (?:[xX]\s*)? (Note that a single + changed to a single * after the \s, so zero whitespace characters will now be accepted.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a process with integrity level high, so that it can
In my main process, i create a ffmpeg child process using CreateProcess(...). I need
Say, I have a collection of text files I need to process (e.g. search
I need to create/append a text file and write some data in that. When
Hi I need to create and write the text file in to /data directory..
I have a file which has text like this: #1#14#ADEADE#CAH0F#0#0..... I need to create
I need to process a HTML content and replace the IMG SRC value with
I need to process the incoming predefined ASN format data(coming from verity of clients
I need to process DICOM formatted medical images and visualize them in 3D, also
I need to process messages in batches, lets say of 10. After 10 transactions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.