How to use re in python3 to print only the desired words

59 Views Asked by At

I would like to print the all the words in the text file excluding the braces, parentheses, commas and "data:". How can I do it with re?

Desired output: aback abaft ......

import re
file = open("D:\Python\Exercises\Dictionary.txt","r")
dummy=file
for data in dummy:
     if(wordlist != re.findall('"{data:"',data)):
     print(data)

Below is the content in the text file {"data":["aback","abaft","abandoned","abashed","aberrant","abhorrent","abiding","abject","ablaze","able","abnormal","aboard","aboriginal","abortive","abounding","abrasive","abrupt","absent","absorbed","absorbing","abstracted","absurd","abundant","abusive","acceptable","accessible","accidental","accurate","acid","acidic"]}

2

There are 2 best solutions below

4
aydow On

It looks like you're reading in json. Try this (if you always expect the data in the above form)

In [260]: import json

In [261]: with open('dict.txt') as f:
     ...:     raw = f.read()
     ...:

In [263]: data = json.loads(raw)

In [265]: for d in data['data']:
     ...:     print(d)
     ...:
0
U13-Forward On

a funny way of doing this:

import re
with open("D:\Python\Exercises\Dictionary.txt","r") as f:
   l=[re.sub('[^a-zA-Z]',' ',i) for i in f.read().split(':')]
   d={k.strip():v.split() for k,v in dict(zip(l[::2],[i for i in l if i not in l[::2]])).items()}
   for i in d['data']:
      print(i)

Output:

aback
abaft
abandoned
abashed
aberrant
abhorrent
abiding
abject
ablaze
able
abnormal
aboard
aboriginal
abortive
abounding
abrasive
abrupt
absent
absorbed
absorbing
abstracted
absurd
abundant
abusive
acceptable
accessible
accidental
accurate
acid
acidic