How to extract only the english words from the list?

2024/7/7 6:03:21

I tried to extract only the English words from the following list:

l = ['0', 'b', 'x14', 'x00', 'x1fP', 'xe0O', 'xd0', 'xea', 'i', 'x10', 'xa2', 'xd8', 'x08', 'x00', '00', 'x9d', 'x14', 'x00', 'x80', 'xcc', 'xbf', 'xb4', 'xdbLB', 'xb0', 'x7f', 'xe9', 'x9a', 'x87', 'xc6AZ', 'x005', 'x00', 'x00', 'x00', 'x00', 'x00yR', 'G', 'x10', 'x00', 'xdc', 'x05', 'xde', 'x05', 'xe2', 'x05', 'xe8', 'x05', 'xdb', 'x05', 'xea', 'x05', 'x00', 'x00', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyRnDyR', 'G', 'x00', 'x00', 'x00', 'xe5E', 'x00', 'x00', 'x00', 'x00', 'xfb', 'x05', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'xe2', 'x0e', 'x00', 'xdc', 'x05', 'xde', 'x05', 'xe2', 'x05', 'xe8', 'x05', 'xdb', 'x05', 'xea', 'x05', 'x00', 'x00', 'x1c', 'x00', 'x80', 'x001', 'x00', 'x00', 'x00', 'x00', 'x00yR', 'G', 'x10', 'x00VBS', '', '', 'x00', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyR', 'GyR', 'G', 'x00', 'x00', 'x00', 'x9e', 'xa5', 'x00', 'x00', 'x00', 'x00K', 'x02', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'xe2', 'x0e', 'x00V', 'x00B', 'x00S', 'x00', 'x00R', 'x00a', 'x00n', 'x00s', 'x00o', 'x00m', 'x00w', 'x00a', 'x00r', 'x00e', 'x00', 'x00', 'x00', 'x00d', 'x00o', 'x00n', 'x00e', 'x00', 'x00', 'x00', 'x00', 'x80', 'x001', 'x00', 'x00', 'x00', 'x00', 'x00yRmG', 'x10', 'x00VBS', '', '', 'x00', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyR', 'GyRmG', 'x00', 'x00', 'x00', 'xb6', 'xba', 'x00', 'x00', 'x00', 'x00', 'xa4', 'x01', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x98w', 'x00V', 'x00B', 'x00S', 'x00', 'x00R', 'x00a', 'x00n', 'x00s', 'x00o', 'x00m', 'x00w', 'x00a', 'x00r', 'x00e', 'x00', 'x00', 'x00', 'x00d', 'x00o', 'x00n', 'x00e', 'x00', 'x00', 'x00', 'x00', 'xa4', 'x002', 'x00c', 'xf1', 'x02', 'x00oRjX', 'Test', 'For', 'SO', 'PDF', 'pdf', 'x00t', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyR', 'GyR', 'G', 'x00', 'x00', 'x00', 'xcf', 'xbc', 'x00', 'x00', 'x00', 'x00z', 'x04', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'xd23', 'x98', 'x00D', 'x00e', 'x00f', 'x00e', 'x00n', 'x00s', 'x00e', 'x00', 'x00R', 'x00u', 'x00l', 'x00e', 'x00', 'x00', 'x00', 'x00V', 'x00B', 'x00S', 'x00', 'x00R', 'x00a', 'x00n', 'x00s', 'x00o', 'x00m', 'x00w', 'x00a', 'x00r', 'x00e', 'x00', 'x00p', 'x00d', 'x00f', 'x00', 'x00', 'x000', 'x00', 'x00', 'x00', '3']

From this list, the words I need are ["Test", "For", "SO", "PDF"].

I tried the following:

for i in range(num_of_values):values = EnumValue(key, i)res = re.findall(r'\w+', str(values))print(res)

Did anyone manage to extract the words?

Answer

You can get it working to some extent with pyenchant library that allows checking if a word is a valid word in a given language. Before checking linguistic validity, you need to check if

  • the word is not empty and is more than one char long
  • the word consists of letters only.

So, in Python, you need to install the pyenchant library first (pip install pyenchant in the terminal/console), and then

import enchant
l = ['0', 'b', 'x14', 'x00', 'x1fP', 'xe0O', 'xd0', 'xea', 'i', 'x10', 'xa2', 'xd8', 'x08', 'x00', '00', 'x9d', 'x14', 'x00', 'x80', 'xcc', 'xbf', 'xb4', 'xdbLB', 'xb0', 'x7f', 'xe9', 'x9a', 'x87', 'xc6AZ', 'x005', 'x00', 'x00', 'x00', 'x00', 'x00yR', 'G', 'x10', 'x00', 'xdc', 'x05', 'xde', 'x05', 'xe2', 'x05', 'xe8', 'x05', 'xdb', 'x05', 'xea', 'x05', 'x00', 'x00', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyRnDyR', 'G', 'x00', 'x00', 'x00', 'xe5E', 'x00', 'x00', 'x00', 'x00', 'xfb', 'x05', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'xe2', 'x0e', 'x00', 'xdc', 'x05', 'xde', 'x05', 'xe2', 'x05', 'xe8', 'x05', 'xdb', 'x05', 'xea', 'x05', 'x00', 'x00', 'x1c', 'x00', 'x80', 'x001', 'x00', 'x00', 'x00', 'x00', 'x00yR', 'G', 'x10', 'x00VBS', '', '', 'x00', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyR', 'GyR', 'G', 'x00', 'x00', 'x00', 'x9e', 'xa5', 'x00', 'x00', 'x00', 'x00K', 'x02', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'xe2', 'x0e', 'x00V', 'x00B', 'x00S', 'x00', 'x00R', 'x00a', 'x00n', 'x00s', 'x00o', 'x00m', 'x00w', 'x00a', 'x00r', 'x00e', 'x00', 'x00', 'x00', 'x00d', 'x00o', 'x00n', 'x00e', 'x00', 'x00', 'x00', 'x00', 'x80', 'x001', 'x00', 'x00', 'x00', 'x00', 'x00yRmG', 'x10', 'x00VBS', '', '', 'x00', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyR', 'GyRmG', 'x00', 'x00', 'x00', 'xb6', 'xba', 'x00', 'x00', 'x00', 'x00', 'xa4', 'x01', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x98w', 'x00V', 'x00B', 'x00S', 'x00', 'x00R', 'x00a', 'x00n', 'x00s', 'x00o', 'x00m', 'x00w', 'x00a', 'x00r', 'x00e', 'x00', 'x00', 'x00', 'x00d', 'x00o', 'x00n', 'x00e', 'x00', 'x00', 'x00', 'x00', 'xa4', 'x002', 'x00c', 'xf1', 'x02', 'x00oRjX', 'Test', 'For', 'SO', 'PDF', 'pdf', 'x00t', 'x00', 't', 'x00', 'x04', 'x00', 'xef', 'xbeyR', 'GyR', 'G', 'x00', 'x00', 'x00', 'xcf', 'xbc', 'x00', 'x00', 'x00', 'x00z', 'x04', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'x00', 'xd23', 'x98', 'x00D', 'x00e', 'x00f', 'x00e', 'x00n', 'x00s', 'x00e', 'x00', 'x00R', 'x00u', 'x00l', 'x00e', 'x00', 'x00', 'x00', 'x00V', 'x00B', 'x00S', 'x00', 'x00R', 'x00a', 'x00n', 'x00s', 'x00o', 'x00m', 'x00w', 'x00a', 'x00r', 'x00e', 'x00', 'x00p', 'x00d', 'x00f', 'x00', 'x00', 'x000', 'x00', 'x00', 'x00', '3']
d = enchant.Dict("en_US") 
output = [el for el in l if len(el)>1 and el.isalpha() and d.check(el)]
>>> output
# => ['Test', 'For', 'SO', 'PDF']
https://en.xdnf.cn/q/120564.html

Related Q&A

Javascript variable with html code regex email matching

This python script is not working to output the email address [email protected] for this case.This was my previous post.How can I use BeautifulSoup or Slimit on a site to output the email address from …

how to send a large array over tcp socket in python? is it possible to send? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Expand Python regex to list of all possible strings [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

How can I group and sum a pandas dataframe? [duplicate]

This question already has answers here:How do I Pandas group-by to get sum?(11 answers)Closed 1 year ago.Ive had a good hunt for some time and cant find a solution so asking here. I have data like so:…

python add value to a list when iterate the list

values = [2,3,4] for v in values:values.append([v,255,255])Why do the statements above never end? I make a mistake in my code. However, I find it will never stop when I execute the code above.

resizing images from 64x64 to 224x224 for the VGG model

Can we resize an image from 64x64 to 256x256 without affecting the resolution is that a way to add zero on new row and column in the new resized output I m working on vgg and I get an error while addin…

Python slicing explained [duplicate]

This question already has answers here:How slicing in Python works(38 answers)Closed 6 years ago.OK I understand the basics, but can someone explain code copied from Gregs answer here:a[1::-1] # the …

Comparison between string characters within a list [duplicate]

This question already has an answer here:How to compare characters of strings that are elements of a list? [duplicate](1 answer)Closed 2 years ago.Having a Python list, containing same length strings,…

Pyspark filling missing dates by group and filling previous values

Spark version 3.0. I have two dataframes. I create one dataframe with date columns using pandas date range. I have a 2nd spark dataframe contains the company name, dates and value. I want to merge the …