python Note: Determine if command line arguments are in the list

Motivation

While studying natural language processing, I generated a text file of the word list, but it was difficult to check the contents because it was large in size, so whether the word I chose appropriately is in the word list I wanted to be able to confirm.

Get command line arguments as a list

I referred to this article on Yukun's Blog.

It seems that the command line arguments are stored in the argv attribute of the sys module.

contain_or_not.py



import sys

r0 = open('vocab.txt','r') #Open file in read mode
vocab  = r0.readlines()    #vocab.txt contains words separated by line breaks
r0.close()

argvs = sys.argv
words = argvs[1:] #argvs[0]Contains the file name at runtime

for word in words:
  if word in vocab:
    print(word + ' is in vocab.')
  else:
    print(word + ' is not in vocab.')

I didn't think about error handling because I only use it, but is it more user-friendly to get an error when a word is not entered?

Problem: Each element of vocal contains a newline symbol

When I typed a word that was clearly contained in vocab.txt, it output not in vocab. Apparently the line feed symbol is bad. Line breaks are easier for humans to see, but is it better not to break files poorly?

~~ In the next post, I will write how to remove the line feed symbol from each element of vocal. ~~ I have written. The modified version of the script is also here. python note: map -do the same for each element of the list

Recommended Posts

python Note: Determine if command line arguments are in the list
Decompose command arguments in one line in Python
Determine if all list elements are present in the dict key
How to receive command line arguments in Python
Check if the characters are similar in Python
Make sure all the elements in the list are the same in Python
In the python command python points to python3.8
Learn the design pattern "Command" in Python
OR the List in Python (zip function)
Check if the URL exists in Python
Read the file line by line in Python
Read the file line by line in Python
Get the EDINET code list in Python
[Python] Read the specified line in the file
Get options in Python from both JSON files and command line arguments
A note I looked up to make a command line tool in Python
Template for creating command line applications in Python
[Python] Sort the list of pathlib.Path in natural sort
Make a copy of the list in Python
Determining if there are birds in the image
Note that Python list comprehensions are always confusing
[Introduction to Udemy Python3 + Application] 67. Command line arguments
[Note] About the role of underscore "_" in Python
How to pass the execution result of a shell command in a list in Python
Sorted list in Python
Check if the string is a number in python
Python note: When the pip command cannot be used
Note the frequently used options in Python + Selenium + Chrome
Specify a subcommand as a command line argument in Python
Implemented List and Bool in Python and SQLite3 (personal note)
Filter List in Python
[python] Get the list of classes defined in the module
Determine the threshold using the P tile method in python
What to do if the print command itself causes an error in Maya python
Things to note when initializing a list in Python
List find in Python
Determine if an attribute is defined in the object
How to pass arguments when invoking python script from blender on the command line
How to check in Python if one of the elements of a list is in another list
[Python] Outputs all combinations of elements in the list
A note that runs an external program in Python and parses the resulting line
[Python] How to output the list values in order
Using the LibreOffice app in Python (1) Where are the macros?
When will the default arguments be bound in python? When variables are bound in closure lazy evaluation.
How to check if the contents of the dictionary are the same in Python by hash value
How to pass the execution result of a shell command in a list in Python (non-blocking version)
[python] How to check if the Key exists in the dictionary
Allow brew install of command line tools made in Python
Get the MIME type in Python and determine the file format
Sort and output the elements in the list as elements and multiples in Python.
Get the number of specific elements in a python list
[Road to intermediate Python] Use if statement in list comprehension
[Note] Import of a file in the parent directory in Python
Decrypt one line of code in Python lambda, map, list
How to get the last (last) value in a list in Python
How to specify command line arguments when debugging in PyCharm
"Let Python do the boring things" exercise ~ Command line mailer ~
[Python] Read command line arguments from file name or stdin
Google search for the last line of the file in Python
Fizzbuzz in Python (in one line)
Download the file in Python