0

I would like to write a Python program that searches and saves the vocabulary definition of an English word.

I have converted a Babylon English-Italian dictionary into a text file.

I would like that re.match() matches the first word of each line, but it doesn't.

I get always: 'not found', namely None for any query (word copied in my code) I use.

The Babylon text file can be found here: https://tempfile.io/en/ba2voaBnDJsn24P/file

Thanks

import clipboard
import json
import re

wordcopied = clipboard.paste()
print(wordcopied)
dictionary = {}

with open("/home/user/Babylon-EI.txt", "r") as source:
    lsource = source.readlines()
    for line in lsource:
        #print(type(line))
        matc = re.match(wordcopied, line, re.MULTILINE | re.DOTALL)
        if matc != None:
            print(line)
            dictionary = {wordcopied:line}
        else:
            print('not found')
            break

I've tried also with re.search, and with multiple flags.. Related questions are all answered regarding flags and blanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mauro
  • 1
  • 2
  • `re.match` only searches at the start of the string, use `re.search`. And `re.escape` the word if it is a literal string. – Wiktor Stribiżew Jan 21 '23 at 20:59
  • thanks for the advice. i already tried re.search. my goal is to in fact find the match at the beginning of each line. I have already done a search on similar issues but these doesn't fit properly my case. – Mauro Jan 22 '23 at 09:46
  • The `if matc != None:` looks suspicious, why not just use `if matc:`? – Wiktor Stribiżew Jan 22 '23 at 10:07
  • Seem your codes search the word one line at a time in a loop and if the first line fails, your codes would break the loop. Probably what you meant is matc = re.match(wordcopied, lsource, re.MULTILINE | re.DOTALL) (without the for loop) – Shiping Jan 22 '23 at 21:18
  • Yes! Shiping! you are right! it was the else statement! what a dummy was I! I beg your pardon! – Mauro Jan 23 '23 at 20:24

0 Answers0