Solved

Case insensitive Find

Posted on 2006-06-21
4
508 Views
Last Modified: 2012-06-21
Python is used to parse report text. Text between certain keywords are stored to Db. Like:

value = reportText[(reportText.find('FINDINGS:'))+9:reportText.find('IMPRESSION:')]
value = value.rstrip()
value = value.lstrip()

Is used to get the text between the FINDINGS: and IMRESSION: and later, store the text in the findings column. I cannot get the other party to protect the report template or send me the data pre-parsed. Often someone will change FINDINGS: to Findings: which breaks the above. I must preserve the case of the reportText.

How can I accomplish the same parsing task but make it so the find is case insensitive?  
0
Comment
Question by:acave
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
ramrom earned 400 total points
ID: 16956094
reportText.upper().find('FINDINGS:')
0
 
LVL 15

Assisted Solution

by:mish33
mish33 earned 100 total points
ID: 16956913
def get_from_report(rpt):
  import re
  m = re.match(r'FINDINGS:\s*(.?*)\s*IMPRESSION:', rpt, re.IGNORECASE)
  if m:
    return m.group(1)

>>> print get_from_report(findings: abc impression:')
abc
0
 
LVL 15

Expert Comment

by:mish33
ID: 16956921
Correction: use re.search instead of re.match
0
 

Author Comment

by:acave
ID: 16971768
Thanks ramrom, love simplicity. Thanks mish33, got me thinking about encapsulation and reuse. I combined your answers to build a good solution.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now