2
$\begingroup$

I want to extract the values of the below text

Pafient Name : Thomas Joseph MRNO : DQ026151?
Doctor : Haneef M An : 513! Gandar : Male
Admission Data : 19-Feb-2V'3‘¥T12:2'$ PM Bill No : IDOGIII.-H-17
Discharge Date : 22-Feb-20$? 1D:5‘F AM Bill Dale : E2-Feb-2017

extract only the values of the field names for example,

Thomas Joseph from the field name Pateint name, similarly for others field names and save the output to excel

Python code for the above

My attempt -

text = pt.image_to_string(img1)
print(text)
s = re.findall(r'\s:\s(\w+)', text)
print (s)
$\endgroup$
  • $\begingroup$ This job would be quite easier if you know what fields you will have in the text. $\endgroup$ – Kiritee Gak Apr 10 '17 at 6:57
  • $\begingroup$ These are the field names. Pafient Name , MRNO , Doctor , An, Gandar , Admission Data, Bill No ,Discharge Date,Bill Dale $\endgroup$ – Shyama Apr 10 '17 at 7:00
  • $\begingroup$ I want to extract the values from these field names..like Patient name is the field name then i need to extract "Thomas Joseph" $\endgroup$ – Shyama Apr 10 '17 at 7:01
  • $\begingroup$ what I am saying is, are these field names always the same or they change from doc to doc or will there be addn of new fields sometimes? $\endgroup$ – Kiritee Gak Apr 10 '17 at 7:04
  • 4
    $\begingroup$ I'm voting to close this question as off-topic because this is just a python regex question and you'd get a quicker answer on StackOverflow. $\endgroup$ – Spacedman Apr 10 '17 at 7:33
1
$\begingroup$

It may not be perfect but does the job almost.

import re
re.findall(r'(?<=: )\w{2}-\w{3}-\d{4}|(?<=: )\d{2}-\w{3}-\w{2}|(?<=: )\s?\w+\s?\w+\s?\w+',data)

#['Thomas Joseph MRNO','DQ026151','Haneef M An','513','Male','19-Feb-2V','IDOGIII','22-Feb-20','E2-Feb-2017']
$\endgroup$
  • $\begingroup$ Ya this seems better. but still cudnt extract all values of the fieldnames $\endgroup$ – Shyama Apr 10 '17 at 9:48
  • $\begingroup$ ['Thomas Joseph MRNO', 'DQ026151', 'Haneef M An', '513', 'Male\nAdmission Data', '19-Feb-2V', 'IDOGIII', '22-Feb-20', 'E2-Feb-2017'] $\endgroup$ – Shyama Apr 10 '17 at 9:51
  • $\begingroup$ above comment was my output. i need still more better output $\endgroup$ – Shyama Apr 10 '17 at 9:52
  • $\begingroup$ Can you please explain me the above code. $\endgroup$ – Shyama Apr 10 '17 at 11:07
  • 1
    $\begingroup$ Check the chat room. $\endgroup$ – Jil Jung Juk Apr 10 '17 at 11:51
1
$\begingroup$

As @spacedman correctly mentioned, this will answered quicker at StackOverflow. But you can use this to create a dictionary like this. There might be a better way but this is a quick work around.

# -*- coding: utf-8 -*-
import re
st = '''Pafient Name : Thomas Joseph MRNO : DQ026151?
Doctor : Haneef M An : 513! Gandar : Male
Admission Data : 19-Feb-2V'3‘¥T12:2'$ PM Bill No : IDOGIII.-H-17
Discharge Date : 22-Feb-20$? 1D:5‘F AM Bill Dale : E2-Feb-2017'''
st = st.decode('utf-8').replace('\n','')+'<eof>'
words = ['Pafient Name','MRNO','Doctor','An','Gandar','Admission Data','PM Bill No','Discharge Date','Bill Dale','<eof>']
print {words[i]:st[st.index(words[i])+len(words[i]):st.index(words[i+1])].replace(':','').strip() for i in range(len(words)-1)}
$\endgroup$
  • $\begingroup$ I tried to execute this.i got this error $\endgroup$ – Shyama Apr 10 '17 at 7:48
  • $\begingroup$ Well, invest some of your time onto it and post that error onto StackOverflow if you are unable to resolve it. $\endgroup$ – Kiritee Gak Apr 10 '17 at 7:58
  • $\begingroup$ File "<ipython-input-6-72de8d228cb9>", line 8 print (words[i]:st[st.index(words[i])+len(words[i]):st.index(words[i+1])].replace(':','').strip() ^ SyntaxError: invalid syntax $\endgroup$ – Shyama Apr 10 '17 at 8:56
  • $\begingroup$ Try it in python 2.7, you might be using 3.x. Either way remove the dictionary comprehension and write a normal loop and try to print the substrings and then debug and find the type of error. This should give u some clue. $\endgroup$ – Kiritee Gak Apr 10 '17 at 9:24
  • $\begingroup$ In which version of Python did u execute the above code.can u show me the output u have got. $\endgroup$ – Shyama Apr 10 '17 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.