Search code examples
pythonstringdictionarysplitregex-group

Splitting a string by multiple keywords and creating a dict


After doing some web scraping i was finally able to get a string from a font body, and it comes out as follows

string = Date:  02/13/2020  Court Time:  1030  Court Room:  0206  Microfilm:  SD000000000

The final thing i need to figure out about my code, and i guess it seems rather trivial at this point, would be to split that string into dictionary pairs where the pairing would look like:

Date:  02/13/2020,
Court Time:  1030,  
Court Room:  0206,  
Microfilm:  SD000000000

I thought of maybe doing something such as:

keywords = ['Date:','Court Time:','Court Room:', 'Microfilm:']
for k in keywords:
            print(string.split())

Using those keywords as delimiters. but it spat this out multiple times

['Date:', '02/13/2020', 'Court', 'Time:', '1030', 'Court', 'Room:', '0206', 'Microfilm:', 'SD000000000']
['Date:', '02/13/2020', 'Court', 'Time:', '1030', 'Court', 'Room:', '0206', 'Microfilm:', 'SD000000000']
['Date:', '02/13/2020', 'Court', 'Time:', '1030', 'Court', 'Room:', '0206', 'Microfilm:', 'SD000000000']
['Date:', '02/13/2020', 'Court', 'Time:', '1030', 'Court', 'Room:', '0206', 'Microfilm:', 'SD000000000']

Solution

  • As per your example:

    s='Date:  02/13/2020  Court Time:  1030  Court Room:  0206  Microfilm:  SD000000000'
    

    Assuming that a double space is your separator:

    sep = '  '
    lst = s.split(sep)
    d = dict(zip(lst[0::2], lst[1::2]))
    

    The output is:

     {'Date:': '02/13/2020',
      'Court Time:': '1030',
      'Court Room:': '0206',
      'Microfilm:': 'SD000000000'}