M Lensch
M Lensch

Reputation: 43

How to print characters from excel cell until it is a capital letter?

I am currently just grabbing the first 60 characters but I want it too take the characters until it find sees a capital letter then moving on to the next cell.

A typical example or cell looks like 'workers comp lawyer Exact match (close variant) None' I want it to stop once it sees the E

    GSt = "Search terms report.xlsx"
    time.sleep(1)
    GWB = openpyxl.load_workbook(GSt)
    Gsheet = GWB['Sheet1']

    #lists out terms
    for cellObj in list(Gsheet.columns)[0]:
         print(cellObj.value[:60])

Upvotes: 0

Views: 90

Answers (1)

ba5h
ba5h

Reputation: 97

You could use regex to split on capital letters and take the first item in the list, like so:

import re

GSt = "Search terms report.xlsx"
    time.sleep(1)
    GWB = openpyxl.load_workbook(GSt)
    Gsheet = GWB['Sheet1']

for cellObj in list(Gsheet.columns)[0]:
    print(re.split('[A-Z]', cellObj.value)[0])

Upvotes: 1

Related Questions