-1

I have a code that read files and compare the content with a user-input with ignoring case-sensitive.

i used the list-comprehension in order to loop through the content and compare with user-input.

The problem is that the list comprehension return an empty list, although the entered word exist. Example:

textContent

Les hiboux Charles Baudelaire

Cycle 3 *

POESIE

Sous les ifs noirs qui les abritent Les hiboux se tiennent rangés Ainsi que des dieux étrangers Dardant leur œil rouge. Ils méditent.

Sans remuer ils se tiendront Jusqu'à l'heure mélancolique Où, poussant le soleil oblique, Les ténèbres s'établiront.

Leur attitude au sage enseigne Qu'il faut en ce monde qu'il craigne Le tumulte et le mouvement ;

L'homme ivre d'une ombre qui passe Porte toujours le châtiment D'avoir voulu changer de place.

Les Fleurs du Mal 1857

Charles Pierre Baudelaire (1821 – 1867) est un poète français.

user-input: charl
word exist : Charles--charle--CHARLE

x=self.lineEditSearch.text()
print(x)
textString=self.ReadingFileContent(Item)

#self.varStr =[c for c in textString if c.islower() or c.isupper() or c.capitalize()]            
self.varStr =[i for i in textString if i.lower() == x.lower()]            

print(self.varStr)
Propy Propy
  • 57
  • 10

2 Answers2

0

If

user_input = "charl"
word_exist = ["Charles","charle","CHARLE","Hello"]

Then

output = [item for item in word_exist if user_input.lower() in item.lower()]
print(output)
# ['Charles', 'charle', 'CHARLE']

Is this what you are looking for?

Shivam Singh
  • 1,584
  • 1
  • 10
  • 9
0

Your problem is, you are only putting in self.varStr members of textString that satisfies i.lower() == x.lower(), which means "being completely the same (case insensitive) with x".

You want to pick up members that contains x.

You can do that by changing i.lower() == x.lower() into i.lower() in x.lower()

Mr.K
  • 559
  • 4
  • 9