I have a lot of words in a text file, each word is not separated by any delimiter, but we can tell the different words because each individual word begins with a capital letter. I want to extract all the words and store them in a list: My python script:
words = ''
with open("words.txt",'r') as mess:
for l in mess.read():
if l.isupper():
words += ','+l
else:
words += l
words = [word.strip() for word in words.split(',') if word]
print(words)
Output:
['Apple', 'Banana', 'Grape', 'Kiwi', 'Raspberry', 'Pineapple', 'Orange', 'Watermelon', 'Mango', 'Leechee', 'Coconut', 'Grapefruit', 'Blueberry', 'Pear', 'Passionfruit']
Inside words.txt (note that there are newlines, and this is only an example of the actual text):
AppleBananaGrapeKiwiRaspberry
PineappleOrangeWatermelonMangoLeecheeCoconutGrapefruit
BlueberryPear
Passionfruit
My code works fine, but I'm wondering if there is a special method python can split a text without a delimiter, only by the capitals. If not, can someone show me more practical way?