Skip to main content

Solve TypeError: expected string or bytes-like object in Python Gorakh Gupta The Crazy Programmer

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

Comments

Popular posts from this blog

Difference between Web Designer and Web Developer Neeraj Mishra The Crazy Programmer

Have you ever wondered about the distinctions between web developers’ and web designers’ duties and obligations? You’re not alone! Many people have trouble distinguishing between these two. Although they collaborate to publish new websites on the internet, web developers and web designers play very different roles. To put these job possibilities into perspective, consider the construction of a house. To create a vision for the house, including the visual components, the space planning and layout, the materials, and the overall appearance and sense of the space, you need an architect. That said, to translate an idea into a building, you need construction professionals to take those architectural drawings and put them into practice. Image Source In a similar vein, web development and design work together to create websites. Let’s examine the major responsibilities and distinctions between web developers and web designers. Let’s get going, shall we? What Does a Web Designer Do?

A guide to data integration tools

CData Software is a leader in data access and connectivity solutions. It specializes in the development of data drivers and data access technologies for real-time access to online or on-premise applications, databases and web APIs. The company is focused on bringing data connectivity capabilities natively into tools organizations already use. It also features ETL/ELT solutions, enterprise connectors, and data visualization. Matillion ’s data transformation software empowers customers to extract data from a wide number of sources, load it into their chosen cloud data warehouse (CDW) and transform that data from its siloed source state, into analytics-ready insights – prepared for advanced analytics, machine learning, and artificial intelligence use cases. Only Matillion is purpose-built for Snowflake, Amazon Redshift, Google BigQuery, and Microsoft Azure, enabling businesses to achieve new levels of simplicity, speed, scale, and savings. Trusted by companies of all sizes to meet

2022: The year of hybrid work

Remote work was once considered a luxury to many, but in 2020, it became a necessity for a large portion of the workforce, as the scary and unknown COVID-19 virus sickened and even took the lives of so many people around the world.  Some workers were able to thrive in a remote setting, while others felt isolated and struggled to keep up a balance between their work and home lives. Last year saw the availability of life-saving vaccines, so companies were able to start having the conversation about what to do next. Should they keep everyone remote? Should they go back to working in the office full time? Or should they do something in between? Enter hybrid work, which offers a mix of the two. A Fall 2021 study conducted by Google revealed that over 75% of survey respondents expect hybrid work to become a standard practice within their organization within the next three years.  Thus, two years after the world abruptly shifted to widespread adoption of remote work, we are declaring 20