Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression - Python - Remove Leading Whitespace

Tags:

python

regex

I search a text file for the word Offering with a regular expression. I then use the start and end points from that search to look down the column and pull the integers. Some instances (column A) have leading white-space I do not want. I want to print just the number (as would be found in Column B) into a file, no leading white-space. Regex in a regex? Conditional?

price = re.search(r'(^|\s)off(er(ing)?)?', line, re.I)
if price:
    ps = price.start()
    pe = price.end()

             A             B
           Offering       Offer
            56.00         55.00 
            45.00         45.55
            65.222        32.00
like image 314
Captain Cretaceous Avatar asked Dec 27 '22 13:12

Captain Cretaceous


1 Answers

You could use strip() to remove leading and trailing whitespaces:

In [1]: ' 56.00  '.strip()
Out[1]: '56.00'
like image 83
NPE Avatar answered Mar 04 '23 20:03

NPE