Python – Java2Blog https://java2blog.com A blog on Java, Python and C++ programming languages Tue, 28 Nov 2023 04:34:58 +0000 en-US hourly 1 https://wordpress.org/?v=6.2.9 https://java2blog.com/wp-content/webpc-passthru.php?src=https://java2blog.com/wp-content/uploads/2022/09/cropped-ICON_LOGO_TRANSPARENT-32x32.png&nocache=1 Python – Java2Blog https://java2blog.com 32 32 Python | cv2 imwrite() Method https://java2blog.com/cv2-imwrite-python/?utm_source=rss&utm_medium=rss&utm_campaign=cv2-imwrite-python https://java2blog.com/cv2-imwrite-python/#respond Sun, 09 Aug 2020 17:34:55 +0000 https://java2blog.com/?p=9772 In this tutorial, we will see how to save an image in your own system using python by using open-cv which exists as cv2 (computer vision) library.

You can use imwrite() method of cv2 library to save an image on your system. To use cv2 library, you need to import cv2 library using import statement.

Now let’s see the syntax and return value of imwrite() method, then we will move on to the examples.

Syntax

cv2.imwrite(path, image)

Parameters

You need to pass two parameters to imwrite() method. Parameters are:

  1. path: Location address where you want to save an image in your system in string form with including the filename.

    here two cases will be possible :

    i) if you want to save an image in the current working directory then we have to mention only the name of the image with their extension like .jpg,.png etc.

    ii) if you want to save an image at somewhere else in your system not in the current working directory then we have to give complete path, also known as, absolute path of the image.

  2. image: It is the image pixel matrix of that image, which you want to save in your system.

Return Value

It returns either True or False. Return True if the image is successfully saved otherwise return False.

cv2 imwrite() method Examples

Now Let’s see the Python code :

# import computer vision library(cv2) in this Program
import cv2

# import os library in this program
import os

# main code
if __name__ == "__main__" :

    # mentioning absolute path of the image
    img_path = "C:\\Users\\user\\Desktop\\flower.jpg"

    # read an image
    image = cv2.imread(img_path)

    print("Before saving an image ,contents of directory shows :")

    # shows the contents of given directory
    print(os.listdir("C:\\Users\\user\\Desktop\\save image"))
    
    # mentioning absolute path 
    save_img_path = "C:\\Users\\user\\Desktop\\save image\\image1.jpg"
    
    # save an image at the given mention path
    cv2.imwrite(save_img_path,image)

    print("After saving an image ,contents of directory shows :")

    # shows the contents of given directory
    print(os.listdir("C:\\Users\\user\\Desktop\\save image"))

Output :

Before saving an image, contents of directory shows :
[] After saving an image, contents of directory shows :
[‘image1.jpg’]

That’s all about cv2.imwrite() Method.

]]>
https://java2blog.com/cv2-imwrite-python/feed/ 0
Get Output from Python Script in Bash https://java2blog.com/bash-get-output-from-python-script/?utm_source=rss&utm_medium=rss&utm_campaign=bash-get-output-from-python-script https://java2blog.com/bash-get-output-from-python-script/#respond Mon, 24 Jul 2023 07:20:05 +0000 https://java2blog.com/?p=24382 Using Substitution Syntax

Use Substitution Syntax ($(...)) to get output from a Python script and store it in a Bash variable.

print("Welcome to Java2Blog!")
result=$(python my_script.py)
echo $result
Welcome to Java2Blog!

We used the command substitution syntax,$(command), to capture the output of the Python script (my_script.py) in a Bash variable (result). Here, the command was python my_script.py, where a Python interpreter executed the script. Finally, we used an echo to display the value of the result variable on the Bash console.

Let’s learn another example where the Python script takes a string value as an argument.

import sys

def display_str(string):
    print(string)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Please provide a string argument.")
    else:
        input_str = sys.argv[1]
        display_str(input_str)
result=$(python my_script_with_args.py "Java2Blog")
echo $result
Java2Blog

Using Backticks

Use backticks to get output from a Python script and store it in a Bash variable.

print("Welcome to Java2Blog!")
result=`python my_script.py`
echo $result
Welcome to Java2Blog!

This snippet is similar to the last example, but we wrapped the python my_script.py command within the backticks rather than enclosing it within the substitution syntax ($(...)).

Will this approach work if the Python script takes a string value as an argument? Let’s learn it below.

import sys

def display_str(string):
    print(string)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Please provide a string argument.")
    else:
        input_str = sys.argv[1]
        display_str(input_str)
result=`python my_script_with_args.py "Java2Blog"`
echo $result
Java2Blog

Using Pipe

We can use a pipe to get output from the Python script to do various things with that output. Let’s learn it below.

Use Pipe with read Command

Use a pipe with the read command to get output from a Python script in Bash variable.

print("Welcome to Java2Blog!")
python my_script.py | read result
echo $result
Welcome to Java2Blog!

In the above example, we used a Python interpreter (python) to execute the Python script and redirected its output to the read command via a pipe. The read command read the received content and stored it in the result variable, which was further used with the echo command to display it on the standard output (Bash console).

Let’s have another example where the Python script needs a string value that must be passed as an argument.

import sys

def display_str(string):
    print(string)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Please provide a string argument.")
    else:
        input_str = sys.argv[1]
        display_str(input_str)
python my_script_with_args.py "Java2Blog" | read result
echo $result
Java2Blog

Use Pipe with tee Command

Use a pipe with the tee command to get output from a Python script on the Bash console without storing it in any variable or file.

print("Welcome to Java2Blog!")
python my_script.py | tee
Welcome to Java2Blog!

Again, we used the Python interpreter to run the script and redirected its output to the tee via a pipe. Then, the tee command displayed the received input on the console.

Note that the tee command in Bash reads standard input and writes it to both standard output and one or multiple files. It is usually part of a pipeline, and any number of commands can precede or follow it.

In the pipeline, the previous command’s output is the following command’s input. Remember, a pipeline is processed from left to right.

Use a pipe with the tee command to get output from a Python script and store it in a variable.

print("Welcome to Java2Blog!")
result=$(python my_script.py | tee)
echo $result
Welcome to Java2Blog!

We used the substitution syntax to capture the output of the entire pipeline (python my_script | tee) and stored it in the result variable.

Are you looking for a solution to store the outcome of the Python script in the specified text file and print it on the console? You can do it by specifying the text file name preceded by the tee command, as demonstrated below.

print("Welcome to Java2Blog!")
python my_script.py | tee result.txt
Welcome to Java2Blog!

Now, open the result.txt file to see its content or use the cat result.txt command to see the following output on the Bash console.

Welcome to Java2Blog!

Let’s take the above approach with a different Python script which takes a string argument; see the following example.

import sys

def display_str(string):
    print(string)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Please provide a string argument.")
    else:
        input_str = sys.argv[1]
        display_str(input_str)
python my_script_with_args.py "Java2Blog" | tee result.txt
Java2Blog
Java2Blog

Similarly, we can use the cat command rather than the tee command, but the cat command can’t store the Python script’s output to a given text file and show it on the console simultaneously. You must use the tee command in that particular use case.

Using Redirection Operator

Use the redirection operator to get the output of a Python script in a text file. This approach is useful when you neither want to display the Python script’s output on the Bash console nor store it in a variable but write to a text file.

print("Welcome to Java2Blog!")
python my_script.py > result.txt
cat result.txt
Welcome to Java2Blog!

In the above example, we used the Python interpreter to run the Python script and redirected its output to the result.txt file using the redirection operator (>). Then, we used the cat command to read and display the content of the result.txt file on the Bash console.

Let’s see another example where a Python script takes a string-type argument.

import sys

def display_str(string):
    print(string)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Please provide a string argument.")
    else:
        input_str = sys.argv[1]
        display_str(input_str)
python my_script_with_args.py "Java2Blog"> result.txt
cat result.txt
Java2Blog

That’s all about how to get output from Python Script in Bash.

]]>
https://java2blog.com/bash-get-output-from-python-script/feed/ 0
Count Unique Values in NumPy Array https://java2blog.com/count-unique-values-numpy-array/?utm_source=rss&utm_medium=rss&utm_campaign=count-unique-values-numpy-array https://java2blog.com/count-unique-values-numpy-array/#respond Thu, 04 May 2023 13:28:03 +0000 https://java2blog.com/?p=21521 1. Introduction

One of the common tasks Numpy Users may encounter is count unique values in Numpy Array that can help in exploring the distribution of Nummy Array. In this article, we will see different ways to count unique values in Numpy Array.

2. Using np.unique() Method with len() Method

Use np.unique() method with len() method to count unique values in Numpy array.

import numpy as np
## generate a numpy array
array = np.array([1, 2, 3, 3, 4, 5, 5, 6])
result = len(np.unique(array))
print(result)

Running the above code will display the following output on the console:

6

np.unique() method finds unique values in the array and len() function counts number of elements in the array.

3. Using np.unique() method to display unique values

Use np.unique() to display unique values in numpy array.

import numpy as np
## generate a numpy array
array = np.array([1, 2, 3, 3, 4, 5, 5, 6])
result = np.unique(array)
print(result)

Running the above code will display the following output on the console:

[1 2 3 4 5 6]

np.unique() method finds unique values in the array and returns sorted unique values.

4. Use np.unique() Method with return_counts as true

Use np.unique() method with return_counts flag as true to count occurrences of each unique element in Numpy array.

Let’s assume we already have installed a Python library named numpy to work with arrays. In case, we can use pip install numpy to install this if we don’t have it.

To count occurrences of each element in the NumPy array:

  • Use np.array() to create a NumPy array.
  • Use np.unique() to find unique values and their count.
  • Use lambda function to execute an expression (u, c), unique_values, count
  • Use map() function to apply lambda() function to every element of unique_values and count.
  • Use dict() to create a dictionary containing key-value pair where key is unique_value and value is the count.
import numpy as np
## generate a numpy array
array = np.array([1, 2, 3, 3, 4, 5, 5, 6])
unique_values, count = np.unique(array, return_counts=True)
result = dict(map(lambda u, c: (u, c), unique_values, count))
print(result)

Running the above code will display the following output on the console:

{1: 1, 2: 1, 3: 2, 4: 1, 5: 2, 6: 1}

We used np.array() to generate a numpy array, which stored unique and redundant values to identify unique elements.

The function np.unique() used the array and return_counts=True as parameters. It created a new array of unique_elements and stored the count of these elements in the count variable.

The variable result is a Python dictionary created with the method dict() that receives the mapping of a lambda() function.

  • The lambda() function is anonymous and has no name.
  • The map() function is a one-line iterator that applied the lambda() function to each element of unique_values and count.
  • The dict() function created a dictionary result with unique_value as the key and count as the value.

5. Count Specific Values in Numpy Array

To count specific values in Numpy Array, We can use np.count_nonzero() method with condition.

Here is an example to count number of elements that are equal to 3:

array = np.array([1, 2, 3, 3, 4, 5, 5, 6])
count_3 =np.count_nonzero(array==3)
print(count_3)

Running the above code will display the following output on the console:

2

6. Count Unique Rows in 2D Array

To count unique rows in 2D array, use axis=0 option with 2D array.

a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]])
np.unique(a, axis=0)

Output:

array([[1, 0, 0], [2, 3, 4]])

7. Conclusion

In this article, we explored different options for count unique value in Numpy Array. We can apply these options in Practice based on our need.

np.unique() with len() function is useful when we want to count unique value. In case we want to count occurrences of each unique value, np.unique with return_counts option is useful.

]]>
https://java2blog.com/count-unique-values-numpy-array/feed/ 0
Create Array of Arrays in Python https://java2blog.com/create-array-of-arrays-python/?utm_source=rss&utm_medium=rss&utm_campaign=create-array-of-arrays-python https://java2blog.com/create-array-of-arrays-python/#respond Thu, 04 May 2023 13:01:25 +0000 https://java2blog.com/?p=21830 Use numpy.array() Function

To create an array of the arrays in Python:

  • Use the np.array() function to create a numpy.ndarray type array of the arrays.
import numpy as np

array1 = np.array([1,2,3])
array2 = np.array([4,5,6])
array3 = np.array([7,8,9])

array = np.array([array1, array2, array3])
print(array)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

The Python library NumPy scientifically computes advanced numerical work. It is a language extension that adds support for large, multi-dimensional arrays and matrices in the Python language.

The numpy library provides multi-dimensional array objects that work like arrays in C but with a Python interface. It can therefore act as a bridge between Python and the low-level languages used for scientific computing.

We used the np.array() function to create three arrays: array1, array2, and array3. After we finished generating the arrays, we used the np.array() function to create an arrays of them.

The np.array() function is a general-purpose tool for creating and manipulating arrays in the Python programming language. It converts any iterable object to an array of the same type.

It is not a function in the same sense as len() or str() is, but a factory function or class. The constructor can take any number of arguments. For example, we used the np.array() function to create a numpy.ndarray type array of array1, array2, and array3.

Manually create array of arrays

You can directly create array of arrays by specifying array elements to np.array() method.

Here is an example:

import numpy as np
array = np.array([[1,2,3],
                 [4,5,6],
                 [7,8,9]])
print(array)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Here, we directly passed 2d array to np.array() method to create array of arrays in Python.

Use numpy.append() Function

To generate an array of the array(s) in Python:

  • Use the np.append() function that appends arrays to generate a numpy.ndarray type array.
import numpy as np

array1 = np.array([1,2,3])
array2 = np.array([4,5,6])
array3 = np.array([7,8,9])

array = np.append(arr=[array1], values=[array2, array3], axis=0)
print(array)
[[1 2 3]
 [4 5 6]
 [7 8 9]]

We already discussed the numpy library and np.array() function while explaining the code snippet for creating an array of arrays using the numpy.array() function.

The np.append() is a built-in function in the numpy library that adds one iterable object to another in Python. It takes arguments as:

  • The arr to add an iterable at its end.
  • The values to add at the end of arr.
  • The axis to append the values along. By default, it flattens the arr and the values.

We used the np.append() function to append array2 and array3 to the end of array1. In addition, we defined axis=0 to make a multi-dimensional array from the arrays instead of flattening them.

]]>
https://java2blog.com/create-array-of-arrays-python/feed/ 0
Convert String List to Integer List in Python https://java2blog.com/convert-string-list-to-integer-list-python/?utm_source=rss&utm_medium=rss&utm_campaign=convert-string-list-to-integer-list-python https://java2blog.com/convert-string-list-to-integer-list-python/#respond Thu, 27 Apr 2023 16:29:18 +0000 https://java2blog.com/?p=23524 Using map() Method

Use the map() method with list() method to convert string list to integer list in Python.

string_list = ['5', '6', '7', '8', '9']
integer_list = list(map(int, string_list))
print(integer_list)
for item in integer_list:
    print(type(item))
[5, 6, 7, 8, 9]
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>

First, we defined a variable named string_list and set its value to a list having five string values. Next, we used the map() method, which applied the int() method to every element in string_list to change its type from str (string) to int (integer). Once the map() method applied int() to all elements in string_list, it returned a map object which we passed to the list() method to convert it into a list and stored in the integer_list variable.

Finally, we used the print() method and passed integer_list as a parameter to print it on the console. We also used a for loop to iterate over the integer_list to check each element’s data type in the integer_list using the type() function. We also printed the data type of each element on the console using the print() method.

In the above example, the map() method applied the int() method, which was specified as a parameter. Note that we do not write parentheses (()) while writing functions as a parameter in the map() method. If you don’t want to use this approach because you are uncomfortable writing functions without (), then the following solution is for you.

string_list = ['5', '6', '7', '8', '9']
integer_list = list(map(lambda i: int(i), string_list))
print(integer_list)
for item in integer_list:
    print(type(item))
[5, 6, 7, 8, 9]
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>

This example is similar to the previous one, but we used the lambda expression (also called the lambda function) this time; it is a small anonymous function which can accept any number of arguments but have one expression only. In the above example, the map() method applied the lambda i: int(i) function to every element in the string_list to convert it from string to integer.

Unlike the previous example, once map() applies the specified function to all elements in string_list, it returns a map object that we passed to the list() method to convert into a list and store it in integer_list variable. Finally, we printed the entire list and data type of each element in integer_list.

In Python 2, using the list() method to convert a map object to a list is not required because you would already have it, but you will not get any error if you use the list() method.

Using for Loop

Use the for loop to convert string list into integer list in Python.

string_list = ['5', '6', '7', '8', '9']
integer_list = []
for item in string_list:
    integer_list.append(int(item))
print(integer_list)
for item in integer_list: print(type(item))
[5, 6, 7, 8, 9]
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>

Again, we initialized a list with five string values representing integers and stored it in the string_list variable. After that, we also defined integer_list and set it to an empty list. Next, we used a for loop to iterate over each item in the string_list. In each iteration, we used the int() method to convert the current item from string to integer and passed it as an argument to the append() method to populate the integer_list.

Finally, we used the print() method to display the entire integer_list and another for loop to show each element’s data type in integer_list.

Using List Comprehension

Use list comprehension to convert string list to integer list in Python.

string_list = ['5', '6', '7', '8', '9']
integer_list = [int(item) for item in string_list]
print(integer_list)
for item in integer_list: print(type(item))
[5, 6, 7, 8, 9]
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>

Here, we used list comprehension to convert strings to integers. Learners find list comprehension challenging but let’s make it easy to understand. The [int(item) for item in string_list] is the list comprehension that creates a list of integers by looping over every element, an item in the string_list.

For every item in string_list, the int(item) was applied to convert item from string to an integer value; this resulting integer value was added to a new list created by list comprehension. Once all elements in string_list have been looped over and converted to integers, a new list of integers was returned by list comprehension, which we stored in the integer_list variable for further use.

Lastly, we used print() to show the entire integer_list and for loop to display each item’s data type in integer_list.

If you have a question at this point and wonder if the map() and lambda functions are doing the same thing, then why have list comprehensions? Because it is a more readable and concise way to do the same thing as lambda and map() functions, particularly useful when we need to do additional operations on every item of the list.

Now, think of a situation where string_list will have string values representing a mixture of integer and float values. How will you convert them to integer values? See the following solution to learn it.

string_list = ['5', '6.0', '7', '8.8', '9']
integer_list = [round(float(item)) for item in string_list]
print(integer_list)
for item in integer_list: print(type(item))
[5, 6, 7, 9, 9]
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>

This code is similar to the previous example, but for this solution, we used the float() method to convert each item to a floating-point number and passed it to the round() method to convert it to the nearest integer number.

Remember, if you have an actual string value (a text value) in your string_list and use any of the solutions demonstrated yet in this article, you will end up with a ValueError. For example, you will get ValueError if you set a string list as string_list = ['5', '6', 'text']. Note that having an actual integer value ( such as string_list = ['5', '6', 7]) in your string_list will not produce any error.

Using eval() and ast.literal_eval() Methods

Use the eval() method with a list comprehension to convert string list to integer list in Python.

string_list = ['5', '6', '7', '8', '9']
integer_list = [eval(item) for item in string_list]
print(integer_list)
for item in integer_list: print(type(item))
[5, 6, 7, 8, 9]
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>

In the above example, we used the eval() method, which took an item as a parameter; note that the item is a string representation of an integer value such as '5'. The eval() returned its corresponding integer value because it evaluates the given expression and executes it if it is a legal Python statement.

Note that we will get NameError if string_list will have any actual string value such as ['5', 'text', '7', '8', '9'] and will get TypeError if string_list will have any actual integer value such as ['5', 6, '7'].

Use the literal_eval() method of the ast module to convert string list to integer list in Python.

import ast
string_list = ['5', '6', '7', '9']
integer_list = [ast.literal_eval(item) for item in string_list]
print(integer_list)
for item in integer_list: print(type(item))
[5, 6, 7, 9]
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>

This code snippet is similar to the previous one, but we used the ast.literal_eval() method to convert all list items from string to integer. Note that we will get ValueError if an actual integer or float value is found in the string_list such as ['5', 6, 6.7].

You might think that if both ast.literal_eval() and eval() functions evaluate the expressions, then why use them separately? Because they have some differences, which make one desirable over the other based on the use case.

The eval() is Python’s built-in function to evaluate the specified string as Python expressions. It is used to evaluate any valid Python expression, such as statements, and can execute any arbitrary code; however, using eval() can be harmful if used with untrusted inputs as it can run malicious code and result in security threats.

On the other side, the ast.literal_eval() is safer than eval(), which evaluates the specified string as a Python expression and returns an object only if it is a literal value (number, tuple, string, list, boolean, dictionary, or None). It does not evaluate statements or arbitrary code, which makes it a safer option while working with untrusted inputs. Evaluating literal values only makes the ast.literal_eval() function less flexible than eval().

Considering performance, the eval() is faster than the ast.literal_eval() because it does not have to check whether the specified expression being evaluated is a literal value.

Now, the point is when to use which method? If you are sure that input is safe and you must evaluate a string as a Python expression, then you can proceed with the eval() method. However, if you are not confident about the input, whether it is safe or not, and you only have to evaluate a literal value, then use ast.literal_eval().

Until this point, we have learned various solutions to convert string list to integer list, but none of them works on different variants of string_list, such as ['5', 6, '7'], ['5', 6, '7', 8.8], and `[‘5’, ‘6’, ‘text’]. So let’s define a function in the following section which can handle all these.

Using User-defined Function

To convert a string list to an integer list in Python:

  • Use the def keyword to define a function.
  • Inside this function, defined in the first step:
    • Use the if statement with the not operator to check if the item is empty.
    • If the item is not empty, use float() to convert it to float; otherwise, raise ValueError.
    • If the item is converted to float, use int() to check if it can be represented as an integer.
    • If the float and integer values are the same, return the integer value; otherwise, float value.
    • If the item can’t be converted to float, return it as it is.
  • Use the map() method to apply the function (defined in the first step) to every item in the string list.
  • Use list() method to convert map object (returned by map()) to list.
  • Use the print() method to display the converted list.
def convert_string_list_to_integer_list(list_item):
    if not list_item:
        return list_item
    try:
        float_number = float(list_item)
        integer_number = int(float_number)
        return integer_number if float_number == integer_number else float_number
    except ValueError:
        return list_item

string_list = ['some', '4', '9.8', 1, 8.8, 'text']
integer_list = list(map(convert_string_list_to_integer_list, string_list))
print(integer_list)
['some', 4, 9.8, 1, 8.8, 'text']

If you also want to round the floating-point numbers to the nearest integers, you can use the round() method:

def convert_string_list_to_integer_list(list_item):
    if not list_item:
        return list_item
    try:
        float_number = round(float(list_item))
        integer_number = int(float_number)
        return integer_number if float_number == integer_number else float_number
    except ValueError:
        return list_item

string_list = ['some', '4', '9.8', 1, 8.8, 'text']
integer_list = list(map(convert_string_list_to_integer_list, string_list))
print(integer_list)
['some', 4, 10, 1, 9, 'text']

That’s all about convert String List to Integer List in Python.

]]>
https://java2blog.com/convert-string-list-to-integer-list-python/feed/ 0
Convert Object to Float in Pandas https://java2blog.com/convert-object-to-float-pandas/?utm_source=rss&utm_medium=rss&utm_campaign=convert-object-to-float-pandas https://java2blog.com/convert-object-to-float-pandas/#respond Tue, 25 Apr 2023 18:04:12 +0000 https://java2blog.com/?p=23528 1. Introduction

Pandas is python library for data analysis and manipulation. One of the common tasks is to convert data type of column from object to float. We can achieve this using astype() or to_numeric() methods.

Method 1: Using astype()

df['column_name'] = df['column_name'].astype(float)

Method 2: Using to_numeric()

df['column_name'] = pd.to_numeric(df['column_name'])

Let’s see each method in detail with examples.

2. Using astype() Method

Use the astype() method to convert one DataFrame column from object to float in pandas.

import pandas as pd

df = pd.DataFrame({
    'students': ['John', 'Mary', 'Martin', 'Sam'],
    'roll_number': ['5', '8', '7', '10'],
    'marks': ['60', '70.2', '75.1', '80']
     })

print("Before changing data type:",df.dtypes,sep='\n')
df['marks'] = df['marks'].astype(float)
print("\nAfter changing data type:",df.dtypes,sep='\n')
Before changing data type:
students       object
roll_number    object
marks          object
dtype: object

After changing data type:
students        object
roll_number     object
marks          float64
dtype: object

Note that marks column has data type of float64.

Use the astype() method to convert multiple columns of DataFrame from object to float in pandas.

import pandas as pd

df = pd.DataFrame({
    'students': ['John', 'Mary', 'Martin', 'Sam'],
    'roll_number': ['5', '8', '7', '10'],
    'marks': ['60', '70.2', '75.1', '80']
     })

print("Before changing data type:",df.dtypes,sep='\n')
df[['marks','roll_number']] = df[['marks', 'roll_number']].astype(float)

#alternatively, we can do as follows
#df= df.astype({'roll_number': float, 'marks': float})
print("\nAfter changing data type:",df.dtypes,sep='\n')
Before changing data type:
students       object
roll_number    object
marks          object
dtype: object

After changing data type:
students        object
roll_number    float64
marks          float64
dtype: object

Use the astype() method to convert the entire DataFrame from object to float in pandas.

import pandas as pd

df = pd.DataFrame({
    'group_number': ['1', '2', '3', '4'],
    'students_per_group': ['5', '8', '7', '10'],
    'score': ['60', '70.2', '75.1', '80']
     })

print("DataFramBefore changing data type:",df.dtypes,sep='\n')
df = df.astype(float)
print("\nAfter changing data type:",df.dtypes,sep='\n')
DataFramBefore changing data type:
group_number          object
students_per_group    object
score                 object
dtype: object

After changing data type:
group_number          float64
students_per_group    float64
score                 float64
dtype: object

We used the astype() method to convert one column, multiple columns and the entire DataFrame’s dtypes from object to float. This method took a float dtype as a parameter to convert to float. Using astype() depends on the use case, whether you are using it to convert one column, multiple columns or an entire DataFrame; you can refer to the above examples for all these scenarios.

Now, think of a situation where we are supposed to convert the whole DataFrame from object to float while one or multiple columns of this DataFrame are not convertible. Will the astype() method still work? Let’s see the following example.

import pandas as pd

df = pd.DataFrame({
    'students': ['John', 'Mary', 'Martin', 'Sam'],
    'roll_number': ['5', '8', '7', '10'],
    'marks': ['60', '70.2', '75.1', '80']
     })

print("DataFramBefore changing data type:",df.dtypes,sep='\n')
df = df.astype(float)
print("\nAfter changing data type:",df.dtypes,sep='\n')
ValueError: could not convert string to float: 'John'

In the above example, we used the astype() method to convert the entire DataFrame from object to float where all columns are not convertible. So, we will get a ValueError as demonstrated above. Now, how can we handle it?

Yes, we can use the errors attribute set to ignore to leave the entire DataFrame as it is if any of its columns is not convertible from object to float. See the following example.

import pandas as pd

df = pd.DataFrame({
    'students': ['John', 'Mary', 'Martin', 'Sam'],
    'roll_number': ['5', '8', '7', '10'],
    'marks': ['60', '70.2', '75.1', '80']
     })

print("DataFramBefore changing data type:",df.dtypes,sep='\n')
df = df.astype(float, errors='ignore')
print("\nAfter changing data type:",df.dtypes,sep='\n')
DataFramBefore changing data type:
students       object
roll_number    object
marks          object
dtype: object

After changing data type:
students       object
roll_number    object
marks          object
dtype: object

But, if you still want to convert the convertible columns and leave those that aren’t, we use the replace() method with astype() as follows.

import pandas as pd

df = pd.DataFrame({
    'students': ['John', 'Mary', 'Martin', 'Sam'],
    'roll_number': ['5', '8', '7', '10'],
    'marks': ['60', '70.2', '75.1', '80']
     })

print("Before changing data type:",df.dtypes,sep='\n')
df = df.replace('[^0-9\.]+', '', regex=True).astype(float, errors='ignore')
print("\nAfter changing data type:",df.dtypes,sep='\n')
Before changing data type:
students       object
roll_number    object
marks          object
dtype: object

After changing data type:
students        object
roll_number    float64
marks          float64
dtype: object

We successfully converted the roll_number and marks columns from object to float but left the students column as it is because the strings are not convertible to float.

We can also use the replace() method with astype() to convert one or multiple columns of a DataFrame from object to float.

3. Using to_numeric() Method

Use the to_numeric() method to convert one DataFrame column from object to float in pandas.

import pandas as pd

df = pd.DataFrame({
    'students': ['John', 'Mary', 'Martin', 'Sam'],
    'roll_number': ['5', '8', '7', '10'],
    'marks': ['60', '70.2', '75.1', '80']
     })

print("Before changing data type:",df.dtypes,sep='\n')
df['marks'] = pd.to_numeric(df['marks'])
print("\nAfter changing data type:",df.dtypes,sep='\n')
Before changing data type:
students       object
roll_number    object
marks          object
dtype: object

After changing data type:
students        object
roll_number     object
marks          float64
dtype: object

Use the to_numeric() method to convert multiple DataFrame columns from object to float in pandas.

import pandas as pd

df = pd.DataFrame({
    'students': ['John', 'Mary', 'Martin', 'Sam'],
    'roll_number': ['5', '8', '7', '10'],
    'marks': ['60', '70.2', '75.1', '80']
     })

print("Before changing data type:",df.dtypes,sep='\n')
df[['marks','roll_number']] = df[['marks', 'roll_number']].apply(pd.to_numeric)
print("\nAfter changing data type:",df.dtypes,sep='\n')
Before changing data type:
students       object
roll_number    object
marks          object
dtype: object

After changing data type:
students        object
roll_number      int64
marks          float64
dtype: object

Use the to_numeric() method to convert the entire pandas DataFrame from object to float where all columns are convertible.

import pandas as pd

group_df = pd.DataFrame({
    'group_number': ['1', '2', '3', '4'],
    'students_per_group': ['5', '8', '7', '10'],
    'score': ['60', '70.2', '75.1', '80']
     })

print("DataFramBefore changing data type:",group_df.dtypes,sep='\n')
group_df = group_df.apply(pd.to_numeric)
print("\nAfter changing data type:",group_df.dtypes,sep='\n')
DataFramBefore changing data type:
group_number          object
students_per_group    object
score                 object
dtype: object

After changing data type:
group_number            int64
students_per_group      int64
score                 float64
dtype: object

The above examples are similar to the code fences learned in the previous section and have the same flow, but we used the pd.to_numeric method this time.

This method converts the object to a numeric data type; it can be a float or integer, depending on the specified value. For example, pd.to_numeric will convert '5' to 5 while '5.0' to 5.0. So, we used the apply() method to apply the pd.to_numeric function to convert multiple columns or complete DataFrame from object to float.

Converting the entire DataFrame, where all columns can not be converted from object to float, will result in the ValueError; see the following example.

import pandas as pd

df = pd.DataFrame({
    'students': ['John', 'Mary', 'Martin', 'Sam'],
    'roll_number': ['5', '8', '7', '10'],
    'marks': ['60', '70.2', '75.1', '80']
     })

print("DataFramBefore changing data type:",df.dtypes,sep='\n')
df = df.apply(pd.to_numeric)
print("\nAfter changing data type:",df.dtypes,sep='\n')
ValueError: Unable to parse string "John" at position 0

We can use the errors = 'ignore' in the apply() method, as shown below, to ignore columns that are not convertible. For example, see the following code snippet.

import pandas as pd

df = pd.DataFrame({
    'students': ['John', 'Mary', 'Martin', 'Sam'],
    'roll_number': ['5', '8', '7', '10'],
    'marks': ['60', '70.2', '75.1', '80']
     })

print("DataFramBefore changing data type:",df.dtypes,sep='\n')
df = df.apply(pd.to_numeric, errors='ignore')
print("\nAfter changing data type:",df.dtypes,sep='\n')
DataFramBefore changing data type:
students       object
roll_number    object
marks          object
dtype: object

After changing data type:
students        object
roll_number      int64
marks          float64
dtype: object

See, this time, the students column is not converted because it is not convertible, and we have ignored it using the errors attribute in the apply() method.

We can also use the apply() method with the lambda function as df['df_column'] = df['df_column'].apply(lambda x: float(x)) to convert one DataFrame column from object to float, but this approach will not work to convert multiple DataFrame or entire DataFrame; for that, we can use pd.to_numeric(), which we have learned already.

4. Replace invalid values with Nan while conversion

In case, we have any invalid values in column and cannot be converted to float, we can use errors parameter with value as coerce, it will convert invalid value to NaN.

import pandas as pd

df = pd.DataFrame({
    'students': ['John', 'Mary', 'Martin', 'Sam'],
    'roll_number': ['5', '8', '7', '10'],
    'marks': ['60', '70.2', 'AB', '80']
     })

df['marks'] = pd.to_numeric(df['marks'],errors='coerce')
print(df)
students  ... marks
0     John  ...  60.0
1     Mary  ...  70.2
2   Martin  ...   **NaN**
3      Sam  ...  80.0

As you can see, AB value in marks column has been converted to NaN.

5. Conclusion

In this article, we explored how to convert object to float in Pandas using astype() and to_numeric() methods.

We can use errors parameter with value as ignore with both methods if some of columns are not convertible to float and we are still trying to convert complete dataframe or non convertible datatype columns to float.

]]>
https://java2blog.com/convert-object-to-float-pandas/feed/ 0
NameError: Name requests Is Not Defined https://java2blog.com/nameerror-name-requests-is-not-defined/?utm_source=rss&utm_medium=rss&utm_campaign=nameerror-name-requests-is-not-defined https://java2blog.com/nameerror-name-requests-is-not-defined/#respond Sat, 15 Apr 2023 06:15:23 +0000 https://java2blog.com/?p=23420 Python has a name error when you try to use a variable or function that is not defined currently. It means that Python has no idea where to locate the requests module or library you are trying to use in your code when you receive the nameError: name 'requests' is not defined error.

There are several reasons why you might encounter the NameError: name 'requests' is not defined error in Python. Among the most common causes are:

  1. The request Module is not Installed: If you are trying to use the requests module in your code, but it is not installed
  2. Missing import statement: If you are trying to use the requests module in your code, but you have not imported it.
  3. Incorrect spelling: If you have misspelt the name of the requests module in your code.
  4. Missing installation: If you have not installed the requests module on your system but trying to import it for further use.

Let’s reproduce the error first which will lead us to possible solutions.

Reproducing NameError in Python

Use requests module in Python 3.x to reproduce the NameError in Python.

import requests
url = "https://www.w3schools.com/html/html_examples.asp"
response = request.get(url)
print(response.text)
NameError                                 Traceback (most recent call last)
Cell In [8], line 3
      1 import requests
      2 url = "https://www.w3schools.com/html/html_examples.asp"
----> 3 response = request.get(url)
      4 print(response.text)
NameError: name 'request' is not defined

The above code starts by importing the requests module, a Python library for sending HTTP requests. It then sets a variable named url to a website URL string. The code then sends an HTTP GET request to the URL using the request.get() method and stores the response object in a variable named response.

Finally, the code prints the content of the response object as text using the response.text attribute. This code uses the requests library to make an HTTP GET request. However, when we try to execute this code, it displays a NameError: name 'request' is not defined' error on the console.

Possible Solutions to Fix NameError in Python

If you encounter the NameError: name 'requests' is not defined error in your Python code, here are some steps you can take to fix it:

Solution 1: pip Module Installation

Ensure you have installed the requests module on your system using the pip package manager.

Install pip using Python 3

We can install the requests module using the following command.

<pre code = Python Title = ‘Install requests Module’>
pip install requests

Check Your import Statement

  1. Ensure you have imported the requests module in your code using the import statement as import requests. See the following example.

<pre code = Python Title = ‘Import requests Module’>
import requests
response = requests.get(‘https://www.w3schools.com/html/html_examples.asp‘)
print(response.text)

In this code, the requests module is imported, and the get() method is used to make an HTTP GETrequest to the specified URL. The response.text is an attribute in the Response object in the requests library that returns the HTTP response body as a string.

  1. Ensure you have spelt the name of the requests module correctly everywhere in your code. The module name is requests, with no capital letters or spaces.

Solution 2: requests Module is Out of Nested Scope

The error message stating requests module is out of nested scope typically occurs when you try to use the requests module inside a nested function or class. See the following example.

def my_function():
import requests
def nested_function():
     response = requests.get('https://www.w3schools.com/html/html_examples.asp')
nested_function()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15364/3784162057.py in <module>
      3 def nested_function():
      4     response = requests.get('https://www.w3schools.com/html/html_examples.asp')
----> 5 nested_function()
~\AppData\Local\Temp/ipykernel_15364/3784162057.py in nested_function()
      2     import requests
      3 def nested_function():
----> 4     response = requests.get('https://www.w3schools.com/html/html_examples.asp')
      5 nested_function()
NameError: name 'requests' is not defined

In this example, the requests module is imported inside the my_function function, and then used inside a nested function nested_function().

When the nested function is called, Python will look for the requests module in the local namespace of the nested_function() first, and since it’s not found there, it will look in the local namespace of the my_function(). Since the requests module is not defined in the my_function() namespace, Python will raise a NameError with the message "requests Module is Out of Nested Scope".

Solution 3: requests Module Imported into the Global

Import requests module in global namespace in Python.

import requests
def my_function():
    def nested_function(requests_module):
        response = requests_module.get('https://www.w3schools.com/html/html_examples.asp')
        print(response.text)
    nested_function(requests)
my_function()

In this example, the requests module is imported in the global namespace and then passed as an argument to the nested_function() function. When the function is called, the requests module is passed as an argument to the function and used as requests_module inside the function.

Considering the above discussion, the NameError: name 'requests' is not defined error occurs when the Python interpreter cannot find the requests library in the current environment. This error can be easily fixed by ensuring the requests module is imported, spelt, and installed correctly.

Following best practices, such as importing modules at the beginning of your code, using descriptive variable names, and keeping dependencies up-to-date, can help you avoid this error in the future.

That’s all about NameError: Name requests Is Not Defined.

]]>
https://java2blog.com/nameerror-name-requests-is-not-defined/feed/ 0
Convert Hex to String in Python https://java2blog.com/python-hex-to-string/?utm_source=rss&utm_medium=rss&utm_campaign=python-hex-to-string https://java2blog.com/python-hex-to-string/#respond Fri, 14 Apr 2023 19:11:57 +0000 https://java2blog.com/?p=23368 1. Introduction

The representation of data in Hexadecimal is commonly used in computer programming. It is used to represent binary data in a human-readable form, as understanding the machine language (i.e., Binary) is difficult for humans. Hexadecimal notation is used in networking protocols, e.g., IPv6 and cryptography, and widely used in graphics, e.g. to represent numbers. Note that hexadecimal uses 16 digits, including 0-9 and A-F, to represent numbers.

Considering the importance of hexadecimal, some programming languages and APIs require string representation of binary data. As a lot of data is in hexadecimal form, so we need to convert the hexadecimal number to a string. This conversion helps to display and manipulate the binary data as text, which makes the task more convenient for humans.

In python language, there are different ways to convert Hex to String.

Method 1: Using bytes.fromhex() method

byte_str = bytes.fromhex(hex_str)            #Convert hex string to bytes
regular_str = byte_str.decode('utf-8')       #Convert bytes to regular string

Method 2: Using binascii module

byte_str = binascii.unhexlify(hex_str)           # Convert hex string to bytes
regular_str = byte_str.decode('utf-8')           # Convert bytes to regular string

Method 3: Using codecs module

byte_str = binascii.unhexlify(hex_str)           # Convert hex string to bytes
regular_str = byte_str.decode('utf-8')           # Convert bytes to regular string

Method 4: Using List comprehension

regular_str = ''.join([chr(int(hex_str[i:i+2], 16)) for i in range(0, len(hex_str), 2)])

Let’s go through each method in detail.

2. Using bytes.fromhex() Method

Use the bytes.fromhex() method to convert a hexadecimal string to a simple string in Python.

hex_str = "48656c6c6f20576f726c64"           #Hex string
byte_str = bytes.fromhex(hex_str)            #Convert hex string to bytes
regular_str = byte_str.decode('utf-8')       #Convert bytes to regular string
print(regular_str)                           #Print Output
Hello World

First, the hex_str is defined, which contains a sequence of characters representing a hexadecimal number. After that, the bytes.fromhex() method is called with hex_str as its argument that converts the string of hexadecimal characters into a byte object. The resulting byte_str variable contains the byte values represented by the hexadecimal characters.

The decode() method is called on the byte_str variable with 'utf-8' as its argument. This method converted the bytes object into a regular string using the UTF-8 encoding. The resulting regular_str variable contains the "Hello World" string. Finally, the regular_str variable is printed to the console using the print() function.

3. Using binascii Module

The binascii Module can be used for hex to String conversion. It follows the same steps as required for the above method. The main difference is at the point of converting a hexadecimal string to bytes.

import binascii                                  #Import module
hex_str = "48656c6c6f20576f726c64"               # Hex string
byte_str = binascii.unhexlify(hex_str)           # Convert hex string to bytes
regular_str = byte_str.decode('utf-8')           # Convert bytes to regular string
print(regular_str)                               # Print Output
Hello World

In this example, first, we imported the binascii module, which provides functions for converting between binary and ASCII formats. Then, the unhexlify() function converts the hex string to a bytes object. Finally, the bytes object is converted to a regular string using the decode() method and printed to the console.

4. Using codecs Module

The codecs‘s decode() method can be used for python hex to String conversion. It takes two arguments – bytes object and encoding type

import codecs                                     #Import module

hex_str = "48656c6c6f20576f726c64" 
hex_str_bytes = bytes(hex_str, encoding='utf-8') # Convert hex string to bytes
binary_string = codecs.decode(hex_str_bytes, "hex") # Convert bytes to regular string
print(str(binary_string, 'utf-8'))
Hello World

In this example, first, we imported the codecs module. Then, the bytes() function converts the hex string to a bytes object. Finally, the bytes object is converted to a regular string using the decode() method and printed to the console.

5. Using List Comprehension

Use list comprehension to convert hexadecimal to string in Python. Unlike the above methods, the hexadecimal string is not converted into bytes but into a decimal integer.

#Hex string
hex_str = "48656c6c6f20576f726c64"

#loop over the hexadecimal string to convert to
#a decimal integer and join the respective ASCII character
regular_str = ''.join([chr(int(hex_str[i:i+2], 16)) for i in range(0, len(hex_str), 2)])

#Print Output
print(regular_str)
Hello World

The list comprehension converted each pair of hexadecimal characters into a decimal integer using the int() function for this code. Then, the integer was converted to its corresponding ASCII character using the chr() function. Finally, joined the resulting list of characters into a single string using the join() method.

Please note that this method works for hex strings that have even number of digits and do not have prefix 0x.

6. Conclusion

All four methods produced the same output: the string "Hello World" as the input string was the same. Therefore, you can try different hexadecimal strings and convert them into regular strings using the above methods.

]]>
https://java2blog.com/python-hex-to-string/feed/ 0
NameError: Name xrange Is Not Defined in Python https://java2blog.com/nameerror-name-xrange-is-not-defined-python/?utm_source=rss&utm_medium=rss&utm_campaign=nameerror-name-xrange-is-not-defined-python https://java2blog.com/nameerror-name-xrange-is-not-defined-python/#respond Fri, 14 Apr 2023 10:18:37 +0000 https://java2blog.com/?p=23425 This NameError: Name 'xrange' Is Not Defined in Python is one of the errors which can occur while using Python. Our objective is to identify the underlying cause of the error and then provide ways to resolve it.

Using xrange() Method to Reproduce NameError

Use the xrange() method to reproduce NameError in Python 3.x.

for x in xrange(1, 5):
    print(x)
Traceback (most recent call last):
  File "/home/jdoodle.py", line 1, in <module>
    for x in xrange(1, 5):
NameError: name 'xrange' is not defined

When someone tries to use the xrange() method in the Python 3.x version, the NameError: Name 'xrange' Is Not Defined error occurs because the interpreter does not recognize it. This xrange() function is unavailable in Python 3.x or later. It was available in Python 2.x, but in later versions, it is updated as the range() method, which works similarly to the xrange() method but has a different keyword.

What could happen if we used the above code in Python 2.x? As we know that the xrange() is a built-in method in Python 2.x version to provide a range of the integers; so, the above code could produce integers from 1 to 4, the ending number in xrange() method is excluded from the output.

The point is resolving the above-mentioned error in Python 3.x if we have to face it. So let’s learn it in the following section.

Possible Solutions to Resolve NameError: Name xrange Is Not Defined in Python

We can use one of the following solutions to resolve the NameError: Name 'xrange' Is Not Defined error.

Use range() Method

If you use Python 3.x or later versions, replace the xrange() method with range() in your code to resolve this error.

for x in range(1, 5):
    print(x)
1
2
3
4

In the above code snippet, the range() method generates integers from 1 up to 5.

The output of range() is similar to the xrange() method output.

Let’s consider a scenario in which we used the range() function to generate a range of integers having a specific difference. In that case, the range() will take three parameters: start, stop, and step, briefly explained below.

Parameters Meaning
start (inclusive) It represents the start of the range we want to generate. By default, it is 0.
stop (exclusive) It represents the end of the range; it will not include in the range.
step It represents the step size or difference between the integers by default step is 1.

See the following example to learn the use of the above-mentioned parameters:

for x in range(0, 10, 2):
    print(x)
0
2
4
6
8

In this example, the range method generated integers from 0 up to 10 with the difference of 2 steps. If you are using Python 2.x or an older version, you can use the xrange() method in the same way, to generate numbers with specific step differences.

Use xrange=range

Another solution to fix the error in Python 3.x is to make these two methods equal, as shown below.

xrange = range
for x in range(5):
    print(x)
0
1
2
3
4

We can observe xrange = range is used in the above example to fix the NameError: Name 'xrange' Is Not Defined in Python error. So, if you are getting this error, add xrange = range in your code, and boom! It will be resolved.

Use past.builtins Library

To resolve this NameError: Name 'xrange' Is Not Defined in Python error, import the built-in methods from the previous versions in Python 3.x using the past.builtins library, as shown below.

from past.builtins import xrange
for x in xrange(5):
    print(x)
0
1
2
3
4

In this example, the xrange method is imported from the past.builtins library, which contains all the built-in methods of previous python versions.

That’s all abput how to fix NameError: Name xrange is Not Defined in Python.

]]>
https://java2blog.com/nameerror-name-xrange-is-not-defined-python/feed/ 0
Call Python Script from Bash with Arguments https://java2blog.com/call-python-script-from-bash-with-arguments/?utm_source=rss&utm_medium=rss&utm_campaign=call-python-script-from-bash-with-arguments https://java2blog.com/call-python-script-from-bash-with-arguments/#respond Fri, 14 Apr 2023 07:07:40 +0000 https://java2blog.com/?p=23405 Python is a high-level language famous for its simplicity, flexibility, and readability. At the same time, Bash is a Unix shell and command language used primarily on Unix and Linux systems. Data analysis, machine learning, web development, and scientific computing tasks are widely performed using python language. On the contrary, the bash is used for the tasks like file manipulation, process management, and system administration.

The python script can be written using IDEs like PyCharm, Spyder, Jupyter Notebook, Visual Studio Code, Sublime Text and IDLE, as the bash is a command language, so the commands are given to the terminal or command prompt.

Though Python and Bash are used for different purposes, python and bash are used together for tasks like running system commands, task automation, and other tasks. The python script can be written in any IDE, and then by using the command line, the bash can call the python script as it becomes difficult for bash to perform tasks like data analysis, file manipulation etc.

For every solution, we need to write a python script and Call the python script through bash. So let’s learn it for each approach below.

Using sys.argv

Create a python file and write a script where two arguments are taken and displayed on the output screen.

import sys
arg1 = sys.argv[1]
arg2 = sys.argv[2]
print("Argument 1:", arg1)
print("Argument 2:", arg2)

The import sys imported the sys module to get the command-line arguments passed to the script. After that, we assigned the first argument to arg1 and the second to arg2. Then, finally, we printed the values of arg1 and arg2 to the console.

The above script will be saved with the name my_code.py (as bash will require to call the script). The values of arg1 and arg2 will be given to the script through the bash command in the next step.

Call the python script my_code.py created in the previous code example. You can use the following command to call a Python script from Bash with arguments.

#!/bin/bash
python my_code.py hello world
Argument 1: hello
Argument 2: world

The shebang #!/bin/bash indicates this is a Bash script. Then, the python command called our Python script my_code.py. Finally, we passed the arguments hello and world.

Using argparse

Create a python file and write a script where two string arguments are taken and displayed on the output screen.

import argparse

#Create argument parser
parser = argparse.ArgumentParser(description='My script')

#Add arguments
parser.add_argument('arg1', type=str, help='Description of arg1')
parser.add_argument('arg2', type=str, help='Description of arg2')

#Parse arguments
args = parser.parse_args()

#Access command-line arguments
arg1 = args.arg1
arg2 = args.arg2

#Print the arguments
print("Argument 1:", arg1)
print("Argument 2:", arg2)

First, import the argparse module and create an argument parser using argparse.ArgumentParser(). We then added two required arguments to the parser using add_argument(). Each argument has a name (arg1, arg2), a type (str), and a help string that describes the argument. After that, we parsed the command-line arguments using parser.parse_args(), which returns an args object containing the values of the arguments.

The above script will be saved with the name my_code.py (as bash will require to call the script). The values of arg1 and arg2 will be given to the script through the bash command in the next step.

Call the python script my_code.py created in the previous code fence. You can use the following command to call a Python script from Bash with arguments.

#!/bin/bash
python my_code.py -a hello -b world
Argument 1: hello
Argument 2: world

The shebang #!/bin/bash indicates this is a Bash script. Then, the python command called our Python script my_code.py. Finally, we passed the arguments hello and world along with -a and -b. The string parameters were passed as the type of arguments were set to str. The code generates the error if a user tries to enter a number.

Use python or python3 to run the my_code.py as per the installed python version. For example, to check the version of python, type python --version.

In the above examples, two arguments are passed to the python script. You can add more parameters or as allowed by the python script.

]]>
https://java2blog.com/call-python-script-from-bash-with-arguments/feed/ 0