{ads}

Solve TypeError: expected string or bytes-like object in Python



April 13, 2022 at 10:39PM

Whenever we are working with our code to develop something and we write some code, then it may be possible our code encounters any type of error. One of those error is Typeerror, which indicates that we have not given the type of data required. For example, if we try to add 3 + “five”, then we will encounter an error named typeerror because the summation between an integer and a string is not supported in python.

Whenever this type of error occurs, we have to make sure that we are passing the same type of data that is required to complete the operation.

So, in the above sum we are required to pass an integer to add in 3.

It will eliminate our error and the operation will be successfully performed.

In this article we are going to see what causes the error named typeerror: expected string or bytes like object. Basically, we will encounter this type of error, when we are working with a regular expression and we are trying to replace any value with some other value.

Example 1:

import pandas as pd
import re

data = pd.DataFrame({'Name':["Alpha", "Beta", 12, "Gamma"]})
print(data)

data['Name'] = [re.sub(str(i),"Omega", i) if i == 12 else i for i in                                                                           data['Name']]
print(data)

Output:

PS C: \ Users \ ASUS \ Desktop \ Crazy Programmer Work > python -u " c : \ Users \ ASUS \ Desktop \ Crazy Programmer Work \ test.py "
    
      Name
0  Alpha
1   Beta
2     12
3  Gamma
Traceback (most recent call last):
  File "c :\ Users \ ASUS \ Desktop \ Crazy Programmer Work \ test.py ",  line 8, in <module>
    data['Name'] = [re.sub(str(i),"Omega", i) if i == 12 else i for i in data['Name']]
  File "c :\ Users \ ASUS \ Desktop \ Crazy Programmer Work \ test.py ", line 8, in <listcomp>
    data['Name'] = [re.sub(str(i),"Omega", i) if i == 12 else i for i in data['Name']]
  File "C : \ Program Files \ Python382 \ lib \ re.py", line 208, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object

Here we have a DataFrame in which there are some data. In the initial stage when we are trying to print it, it is printed successfully.

Let us suppose we want to change the value of 12 in the Name column.

When we are trying to change it, it is giving the same error as we have discussed earlier.

So we have to check our code that whether the bug is there. As I described earlier this error occurs when we are trying to give any other datatype value different from its normal type which is required in the operation.

Here we are using a regular expression to replace the integer value with a string value.

So, we have to make a simple change in our code to execute it successfully.

As we can see from the documentation of regular expression that re.sub() function required its third parameter as a string but in the code we have passes it as integers.

Solution:

To resolve it, we have to pass the third parameter named I as a string. We have to write str(i) instead of I to remove this error.

import pandas as pd
import re

data = pd.DataFrame({'Name':["Alpha", "Beta", 12, "Gamma"]})
print(data)

data['Name'] = [re.sub(str(i),"Omega", str(i)) if i == 12 else i for i in data['Name']]
print(data)

Output:

PS C: \ Users \ ASUS \ Desktop \ Crazy Programmer Work > python -u " c : \ Users \ ASUS \ Desktop \ Crazy Programmer Work \ test.py "
    Name
0  Alpha
1   Beta
2     12
3  Gamma
    Name
0  Alpha
1   Beta
2  Omega
3  Gamma

As we can see that after passing the third parameter as string, we are successfully able to perform the desired operation using regular expression.

We have successfully changed the value of integer 12 to string Omega.

Example 2:

import re
list1 = ['Alpha', 'Beta', 'Gamma', 40, 30]

list1 = [re.sub(str(item), "this", item) if item in range(10, 41) else item for item in list1 ]

print(list1)

Output:

PS C: \ Users \ ASUS \ Desktop \ Crazy Programmer Work > python -u " c : \ Users \ ASUS \ Desktop \ Crazy Programmer Work \ test.py "
Traceback (most recent call last):
  File "c :\ Users \ ASUS \ Desktop \ Crazy Programmer Work \ test.py ", line 59, in <module>
    list1 = [re.sub(str(item), "this", item) if item in range(10, 41) else item for item in list1 ]
  File "c :\ Users \ ASUS \ Desktop \ Crazy Programmer Work \ test.py ", line 59, in <listcomp>
    list1 = [re.sub(str(item), "this", item) if item in range(10, 41) else item for item in list1 ]
  File "C: \ Program Files \ Python382 \ lib \ re.py ", line 208, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object

In this example, we are trying to replace some values in the list which are integers ranging from 10 to 40. We are performing this operation using sub() function in a regular expression.

As we can see that we have encountered with the same error. Whenever we are working with sub() function, then we have to cross check that we are passing the correct argument type else we will encounter the same error and it is a little bit difficult to find where the exact error is.

As we can see that we are passing the third argument as an integer that why we have encountered the error.

Solution:

We can resolve this error by passing the third argument as string type by converting it into a string.

Let’s see the corrected code and its output.

import re
list1 = ['Alpha', 'Beta', 'Gamma', 40, 30]

list1 = [re.sub(str(item), "this", str(item)) if item in range(10, 41) else item for item in list1 ]

print(list1)

Output:

PS C: \ Users \ ASUS \ Desktop \ Crazy Programmer Work > python -u " c : \ Users \ ASUS \ Desktop \ Crazy Programmer Work \ test.py "
['Alpha', 'Beta', 'Gamma', 'this', 'this']

As we can see that after passing the third argument as a string, we have successfully fixed the error which was shown earlier.

Conclusion

This type of error mostly occurs when we are working with sub() function in a regular expression. This work of this function is to replace a given pattern with some string. We have to be careful when we are passing its arguments. We have to check which type of argument is required to perform the operation. Whenever we pass the argument of other type which is not required then we will encounter this type of error.

The post Solve TypeError: expected string or bytes-like object in Python appeared first on The Crazy Programmer.



from The Crazy Programmer https://ift.tt/TNdb79B

0 Response to "Solve TypeError: expected string or bytes-like object in Python"

Post a Comment

Article Top Ads

Central Ads Article 1

Middle Ads Article 2

Article Bottom Ads