Regex on a Dictionary

Mark Lawrence breamoreboy at gmail.com
Tue Feb 13 13:59:16 EST 2018


On 13/02/18 18:08, Stanley Denman wrote:
> On Tuesday, February 13, 2018 at 9:41:14 AM UTC-6, Mark Lawrence wrote:
>> On 13/02/18 13:11, Stanley Denman wrote:
>>> I am trying to performance a regex on a "string" of text that python isinstance is telling me is a dictionary.  When I run the code I get the following error:
>>>
>>> {'/Title': '1F:  Progress Notes  Src.:  MILANI, JOHN C Tmt. Dt.:  05/12/2014 - 05/28/2014 (9 pages)', '/Page': IndirectObject(465, 0), '/Type': '/FitB'}
>>>
>>> Traceback (most recent call last):
>>>     File "C:\Users\stand\Desktop\PythonSublimeText.py", line 9, in <module>
>>>       x=MyRegex.findall(MyDict)
>>> TypeError: expected string or bytes-like object
>>>
>>> Here is the "string" of code I am working with:
>>
>> Please call it a dictionary as in the subject line, quite clearly it is
>> not a string in any way, shape or form.
>>
>>>
>>> {'/Title': '1F:  Progress Notes  Src.:  MILANI, JOHN C Tmt. Dt.:  05/12/2014 - 05/28/2014 (9 pages)', '/Page': IndirectObject(465, 0), '/Type': '/FitB'}
>>>
>>> I want to grab the name "MILANI, JOHN C" and the last date "-mm/dd/yyyy" as a pair such that if I have  X numbers of string like the above I will end out with N pairs of values (name and date)/  Here is my code:
>>>    
>>> import PyPDF2,re
>>> pdfFileObj=open('x.pdf','rb')
>>> pdfReader=PyPDF2.PdfFileReader(pdfFileObj)
>>> Result=pdfReader.getOutlines()
>>> MyDict=(Result[-1][0])
>>> print(MyDict)
>>> print(isinstance(MyDict,dict))
>>> MyRegex=re.compile(r"MILANI,")
>>> x=MyRegex.findall(MyDict)
>>> print(x)
>>>
>>> Thanks in advance for any help.
>>>
>>
>> Was the string methods solution that I gave a week or so ago so bad that
>> you still think that you need a regex to solve this?
>>
>> -- 
>> My fellow Pythonistas, ask not what our language can do for you, ask
>> what you can do for our language.
>>
>> Mark Lawrence
> 
> My Apology Mark.  You took the time to give me the basis of a non-regex solution and I had not taken the time to fully review your answer.Did not understand it at first blush, but I think now I do.
> 

Accepted :)

IIRC you might need a small tweak or two but certainly the foundations 
were there.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list