1

Python learner here. So I have a wordlist.txt file, one word on each line. I want to filter out specific words starting and ending with specific letters. But in my wordlist.txt, words are listed with their occurrence numbers.

For example:

food 312
freak 36
cucumber 1

Here is my code

wordList = open("full.txt","r", encoding="utf-8")
word = wordList.read().splitlines()

for i in word:
    if i.startswith("h") and i.endswith("e"):
        print(i)

But since each item in the list has numbers at the end I can't filter correct words. I could not figure out how to omit those numbers.

aydgn
  • 167
  • 1
  • 8
  • You can use python's split() function which will split your line by "space" charachter. – Fərid Qənbərli May 20 '20 at 11:31
  • Split each word by whitespace, into the actual word and the number, then ignore the number: `for word in words: actual, *_ = word.split()` and so on. – 9769953 May 20 '20 at 11:32

2 Answers2

1

Try splitting the line using space as the delimiter and use the first value [0] which is the word in your case

for i in word:
    if i.split(" ")[0].startswith("h") and i.split(" ")[0].endswith("e"):
        print(i.split(" ")[0])

Or you can just peform the split once as

for i in word:
    w = i.split(" ")[0]
    if w.startswith("h") and w.endswith("e"):
        print(w)

EDIT: Based on the comment below, you may want to use no argument or None to split in case there happen to be two spaces or a tab as a field delimiter.

w = i.split()[0]
Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • 1
    You may want to use no argument or None to split, in case there happen to be two spaces or a tab as a field delimiter. – 9769953 May 20 '20 at 11:39
  • @00 : Added your suggestion. Thanks – Sheldore May 20 '20 at 11:42
  • This solution filters the words correctly but numbers are still there. – aydgn May 20 '20 at 11:42
  • @aydgn Then `print(w)` instead of `print(i)`… – Melebius May 20 '20 at 11:42
  • @aydgn : yes, print (w) – Sheldore May 20 '20 at 11:44
  • That solved my problem. I don't understand what [0] does. I'm just a beginner so can you send my any article about it? Thanks Sheldore and @Melebius. :) – aydgn May 20 '20 at 11:54
  • 1
    @aydgn : when you split, it results in 2 strings, the first one is the word and the second one is the number. The 0 and 1 are the indices for these two strings. To access first, you use `[0]` index and to access the second, you use `[1]`index. Read [this document](https://docs.python.org/3/tutorial/datastructures.html) – Sheldore May 20 '20 at 12:02
0

Try this

str = "This must not b3 delet3d, but the number at the end yes 12345"
str = re.sub(" \d+", "", str)

The str will be = "This must not b3 delet3d, but the number at the end yes"

Melebius
  • 6,183
  • 4
  • 39
  • 52