Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Match substring using regex
#7
BTW, here is a solution how to solve problem using ordinary Python staff (sure, I'm aware that is probably far from optimal Rolleyes )
video_info = 'Réalisation :\nPierre Lazarus\nScénario :\nEmmanuelle Moreau\nNoémie Parreaux\nProduction :\nFilmakademie Baden-Württemberg\nSWR\nARTE\nProducteur/-trice :\nGiacomo Vernetti Prot\nJennifer Miola\nImage :\nHovig Hagopian\nMontage :\nMathieu Pluquet\nMusique :\nLouis-Ronan Choisy'
video_info_list = video_info.split('\n')

list_keys = []
list_values = []
for item in video_info_list:
    if item[-1] == ':':
        list_keys.append(item[:-2])
        new_key = True
    else:
        if new_key:
            list_values.append(item)
        else:
            list_values[-1] = list_values[-1] + ', ' + item
        new_key = False

video_dict = dict(zip(list_keys, list_values))
for k, v in video_dict.items():
    print(f"{k:<20}{v}")
Reply


Messages In This Thread
Match substring using regex - by Pavel_47 - Jul-17-2022, 12:36 PM
RE: Match substring using regex - by Pavel_47 - Jul-17-2022, 03:25 PM
RE: Match substring using regex - by snippsat - Jul-17-2022, 06:48 PM
RE: Match substring using regex - by Pavel_47 - Jul-17-2022, 10:45 PM
RE: Match substring using regex - by bowlofred - Jul-18-2022, 07:16 AM
RE: Match substring using regex - by Pavel_47 - Jul-18-2022, 07:40 AM
RE: Match substring using regex - by Pavel_47 - Jul-18-2022, 07:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 567 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Facing issue in python regex newline match Shr 6 1,357 Oct-25-2023, 09:42 AM
Last Post: Shr
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,084 Apr-08-2023, 06:36 PM
Last Post: Winfried
  Failing regex, space before and after the "match" tester_V 6 1,219 Mar-06-2023, 03:03 PM
Last Post: deanhystad
  Regex pattern match WJSwan 2 1,298 Feb-07-2023, 04:52 AM
Last Post: WJSwan
  ValueError: substring not found nby2001 4 7,991 Aug-08-2022, 11:16 AM
Last Post: rob101
  Substring Counting shelbyahn 4 6,162 Jan-13-2022, 10:08 AM
Last Post: krisputas
  Match key-value json,Regex saam 5 5,458 Dec-07-2021, 03:06 PM
Last Post: saam
  Python Substring muzikman 4 2,339 Dec-01-2020, 03:07 PM
Last Post: deanhystad
  regex.findall that won't match anything xiaobai97 1 2,051 Sep-24-2020, 02:02 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020