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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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



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:PythonExercisesDictionary.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"]





Just parse it as JSON and get the array obj["data"] and iterate through and print each item. There is no need to use regular expressions for this.
– Spencer Wieczorek
Aug 6 at 1:48



obj["data"]




2 Answers
2



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


json


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)
...:





json must be imported first: import json.
– Graham
Aug 6 at 1:53


json


import json





added the json import
– aydow
Aug 6 at 1:57





Thank very much. Is there an indicator whether a re or a json is preferred for this task?
– user234568
Aug 6 at 1:58





as mentioned in the question i would use json if you are receiving a json string
– aydow
Aug 6 at 2:00



a funny way of doing this:


import re
with open("D:PythonExercisesDictionary.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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard