Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 18906

Why does regular exp. give me an empty dictionary [closed]

$
0
0

Implement a function to parse a string containing data in text format. Explore the details of the format using the examples below. Return the result as a dictionary.

Ex. 1

Input:

\begin {{ xear_496<=list( -6929 -6300 694). }} {{ cetean_212 <= list(-9690 -6566 2845 ). }} {{inarbi<=list( -9509 -8236 3665 2698 ).}}\end

Output:

{'xear_496': \[-6929, -6300, 694\],'cetean_212': \[-9690, -6566, 2845\],'inarbi': \[-9509, -8236, 3665, 2698\]}

Ex. 2

Input:

\begin {{arar <=list(1074 -7143 -1607 -8481 ). }} {{ inater <=list(-8208 2392 ). }} {{ erceare <=list(366 -5229 8399 ).}} {{ arer <=list( -2643 -4908 -7268 7210 ). }} \end

Output:

{'arar': \[1074, -7143, -1607, -8481\],'inater': \[-8208, 2392\],'erceare': \[366, -5229, 8399\],'arer': \[-2643, -4908, -7268, 7210\]}
import redef main(input_string):    result = {}    item = re.compile('{{\s*(\w+)\s*<=\s*list\((.*?)\)\s*}}')    num = re.compile('-?\d+')    matches = item.findall(input_string)    for key, values_str in matches:        values = num.findall(values_str)        result[key] = values    return resultinput_string = "\\begin {{ xear_496<=list( -6929 -6300 694). }} {{ cetean_212 <= list(-9690 -6566 2845 ). }} {{inarbi <=list( -9509 -8236 3665 2698 ).}}\\end"print(main(input_string))

Please help me understand why my code or my regular expression doesn't work.


Viewing all articles
Browse latest Browse all 18906


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>