Expecting string or bytes object ошибка

After much research I cannot figure out why I receive this error in my code.

I’m trying to export a Pandas Dataframe to my Oracle table. I have successfully done this hundreds of times on other data tables but this one keeps producing errors.

Here is my Dataframe, which I read in with pd.read_excel and appended three of my own columns with simple df['column_name'] = variable commands:

S USTAINABLE H ARVEST S ECTOR| QUOTA LISTING APRIL 16 2013 Unnamed: 1  
1                                                DATE           TRADE ID   
2                                            04/02/13             130014   
3                                                   0                  0   
4                                                   0                  0   
5                                                   0                  0   
6                                 FY13 QUOTA – TO BUY                  0   
7                                                DATE           TRADE ID   
8                                             3/26/13             130006   
9                                              4/9/13             130012   
10                                            3/26/13             130007   
11                                            3/26/13             130001   
12                                            3/26/13             130009   
13                                             4/9/13             130013   
14                                            3/26/13             130010   
15                                            3/26/13             130008   
16                                            3/26/13             130011   
17                                                  1                  0   

         Unnamed: 2     Unnamed: 3                     Unnamed: 4 email_year  
1   AVAILABLE STOCK         AMOUNT                      BUY PRICE       2013   
2        WINTER SNE          12000            TRADE IN RETURN FOR       2013   
3                 0              0                   HADDOCK GOM,       2013   
4                 0              0             YELLOWTAIL GOM, OR       2013   
5                 0              0                 WITCH - OFFERS       2013   
6                 0              0                              0       2013   
7     DESIRED STOCK         AMOUNT                      BUY PRICE       2013   
8           COD GBE            ANY                         OFFERS       2013   
9           COD GBW  UP TO 100,000                            0.3       2013   
10          COD GBW            ANY                         OFFERS       2013   
11          COD GOM        INQUIRE                            1.5       2013   
12        WINTER GB            ANY                         OFFERS       2013   
13       WINTER SNE  UP TO 100,000                            0.3       2013   
14       WINTER SNE            ANY                         OFFERS       2013   
15    YELLOWTAIL GB            ANY                         OFFERS       2013   
16   YELLOWTAIL GOM            ANY  TRADE FOR GB STOCKS -nOFFERS       2013   
17                0              0                              0       2013   

   email_month email_day  
1            4        16  
2            4        16  
3            4        16  
4            4        16  
5            4        16  
6            4        16  
7            4        16  
8            4        16  
9            4        16  
10           4        16  
11           4        16  
12           4        16  
13           4        16  
14           4        16  
15           4        16  
16           4        16  
17           4        16  

My code fails on the export line cursor.executemany(sql_query, exported_data) with the error:

Traceback (most recent call last):
  File "Z:Codesuccessful_excel_pdf_code.py", line 74, in <module>
    cursor.executemany(sql_query, exported_data)
TypeError: expecting string or bytes object

Here is my relevant code:

df = pd.read_excel(file_path)


df = df.fillna(0)
df = df.ix[1:]


cursor = con.cursor()
exported_data = [tuple(x) for x in df.values]
#exported_data = [str(x) for x in df.values]
#print("exported_data:", exported_data)

sql_query = ("INSERT INTO FISHTABLE(date_posted, stock_id, species, pounds, advertised_price, email_year, email_month, email_day, sector_name, ask)" "VALUES(:1, :2, :3, :4, :5, :6, :7, :8, 'Sustainable Harvest Sector', '1')")

cursor.executemany(sql_query, exported_data)

con.commit() #commit to database

cursor.close()
con.close()

Here is a printout of exported_data:

[('DATE', 'TRADE ID', 'AVAILABLE STOCK', 'AMOUNT', 'BUY PRICE', '2013', '4', '16'), ('04/02/13', 130014, 'WINTER SNE', 12000, 'TRADE IN RETURN FOR', '2013', '4', '16'), (0, 0, 0, 0, 'HADDOCK GOM,', '2013', '4', '16'), (0, 0, 0, 0, 'YELLOWTAIL GOM, OR', '2013', '4', '16'), (0, 0, 0, 0, 'WITCH - OFFERS', '2013', '4', '16'), ('FY13 QUOTA – TO BUY', 0, 0, 0, 0, '2013', '4', '16'), ('DATE', 'TRADE ID', 'DESIRED STOCK', 'AMOUNT', 'BUY PRICE', '2013', '4', '16'), ('3/26/13', 130006, 'COD GBE', 'ANY', 'OFFERS', '2013', '4', '16'), ('4/9/13', 130012, 'COD GBW', 'UP TO 100,000', 0.3, '2013', '4', '16'), ('3/26/13', 130007, 'COD GBW', 'ANY', 'OFFERS', '2013', '4', '16'), ('3/26/13', 130001, 'COD GOM', 'INQUIRE', 1.5, '2013', '4', '16'), ('3/26/13', 130009, 'WINTER GB', 'ANY', 'OFFERS', '2013', '4', '16'), ('4/9/13', 130013, 'WINTER SNE', 'UP TO 100,000', 0.3, '2013', '4', '16'), ('3/26/13', 130010, 'WINTER SNE', 'ANY', 'OFFERS', '2013', '4', '16'), ('3/26/13', 130008, 'YELLOWTAIL GB', 'ANY', 'OFFERS', '2013', '4', '16'), ('3/26/13', 130011, 'YELLOWTAIL GOM', 'ANY', 'TRADE FOR GB STOCKS -nOFFERS', '2013', '4', '16'), (1, 0, 0, 0, 0, '2013', '4', '16')]

1) I thought the error could be from a lot of NaNs being scattered throughout the Dataframe, so I replaced them with 0’s and it still fails.

2) I then thought the error could be from trying to export the first couple rows which held no valuable information, so I deleted the first row with df = df.ix[1:] but it still fails.

3) I also thought it could be failing because of the values in my email_year/month/day columns, so I changed them all to strings before putting them into my Dataframe, but it still fails.

4) I tried changing the exported_data command to a str instead of a tuple but that only changed the error to cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number. Also, it has always worked fine as a tuple when exporting other Dataframes.

5) I thought the error could be from my Oracle columns not allowing either numbers or letters, but they are all set to all VarChar2 so that isn’t the cause of the error either.

I’d appreciated any help solving this, thanks.

Как исправить: ошибка типа: ожидаемая строка или байтовый объект

  • Редакция Кодкампа

17 авг. 2022 г.
читать 1 мин


Одна ошибка, с которой вы можете столкнуться при использовании Python:

TypeError : expected string or bytes-like object

Эта ошибка обычно возникает, когда вы пытаетесь использовать функцию re.sub() для замены определенных шаблонов в объекте, но объект, с которым вы работаете, не состоит полностью из строк.

В следующем примере показано, как исправить эту ошибку на практике.

Как воспроизвести ошибку

Предположим, у нас есть следующий список значений:

#define list of values
x = [1, 'A', 2, 'B', 5, 'C', 'D', 'E']

Теперь предположим, что мы пытаемся заменить каждую небукву в списке пустой строкой:

import re

#attempt to replace each non-letter with empty string
x = re. sub('[^a-zA-Z]', '', x)

TypeError : expected string or bytes-like object

Мы получаем ошибку, потому что в списке есть определенные значения, которые не являются строками.

Как исправить ошибку

Самый простой способ исправить эту ошибку — преобразовать список в строковый объект, заключив его в оператор str() :

import re

#replace each non-letter with empty string
x = re. sub('[^a-zA-Z]', '', str (x))

#display results
print(x)

ABCDE

Обратите внимание, что мы не получили сообщение об ошибке, потому что использовали функцию str() для первого преобразования списка в строковый объект.

Результатом является исходный список, в котором каждая небуква заменена пробелом.

Примечание.Полную документацию по функции re.sub() можно найти здесь .

Дополнительные ресурсы

В следующих руководствах объясняется, как исправить другие распространенные ошибки в Python:

Как исправить KeyError в Pandas
Как исправить: ValueError: невозможно преобразовать число с плавающей запятой NaN в целое число
Как исправить: ValueError: операнды не могли транслироваться вместе с фигурами

Posted on Dec 29, 2022


Python TypeError: expected string or bytes-like object commonly occurs when you pass a non-string argument to a function that expects a string.

To solve this error, you need to make sure you are passing a string or bytes-like object as the argument of that function.

Two functions that commonly cause this error are:

  • re.sub()
  • re.findall()

Let’s see how to solve the error for both functions in this article.

Solve re.sub() causing TypeError: expected string or bytes-like object

The re.sub() function in Python is used to replace a pattern in a string with a replacement value.

Here’s the function signature:

re.sub(pattern, replacement, string)

Python expects a string as the third argument of the function.

When you run the following code:

import re

result = re.sub("world", "there", 1234)  # ❗️TypeError

Python responds with an error as shown below:

To solve this TypeError, you need to convert the third argument of the function to a string using the str() function:

result = re.sub("world", "there", str(1234))  # ✅

print(result)  # 1234

This solution also works when you pass a list as the third argument of the re.sub() function.

Suppose you want to replace all number values in a list with an X as shown below:

import re

list = [0, "A", 2, "C", 3, "Z"]

list_as_string = re.sub("[0-9]+", "X", list)  # ❗️

print(str)

The call to sub() function will cause an error because you are passing a list.

To solve this error, you need to convert the list to a string with the str() function.

From there, you can convert the string back to a list with the strip() and split() functions if you need it.

Consider the example below:

import re

list = [0, "A", 2, "C", 3, "Z"]

# 👇 Convert list to string with str()
list_as_string = re.sub("[0-9]+", "X", str(list))


# The following code converts the string back to list

# 👇 Remove quotation mark from the string
list_as_string = re.sub("'", "", list_as_string)

# 👇 use strip() and split() to create a new list
new_list = list_as_string.strip('][').split(', ')

print(new_list)
# ['X', 'A', 'X', 'C', 'X', 'Z']

And that’s how you solve the error when using the re.sub() function.

Solve re.findall() causing TypeError: expected string or bytes-like object

Python also raises this error when you call the re.findall() function with a non-string value as its second argument.

To solve this error, you need to convert the second argument of the function to a string as shown below:

import re

list = [0, "A", 2, "C", 3, "Z"]

# Convert list to string with str()
matches = re.findall("[0-9]+", str(list))

print(matches)
# ['0', '2', '3']

The re.findall() function already returns a list, so you can print the result as a list directly.

Conclusion

To conclude, the Python TypeError: expected string or bytes-like object error occurs when you try to pass an object of non-string type as an argument to a function that expects a string or bytes-like object.

To solve this error, you need to make sure that you are passing the correct type of object as an argument to the function.

Two functions that commonly cause this error are the re.sub() and re.findall() functions from the re module.

In some cases, you may need to convert the object to a string using the str() function when passing it as an argument.


One error you may encounter when using Python is:

TypeError: expected string or bytes-like object

This error typically occurs when you attempt to use the re.sub() function to replace certain patterns in an object but the object you’re working with is not composed entirely of strings.

The following example shows how to fix this error in practice.

How to Reproduce the Error

Suppose we have the following list of values:

#define list of values
x = [1, 'A', 2, 'B', 5, 'C', 'D', 'E']

Now suppose we attempt to replace each non-letter in the list with an empty string:

import re

#attempt to replace each non-letter with empty string
x = re.sub('[^a-zA-Z]', '', x)

TypeError: expected string or bytes-like object

We receive an error because there are certain values in the list that are not strings.

How to Fix the Error

The easiest way to fix this error is to convert the list to a string object by wrapping it in the str() operator:

import re

#replace each non-letter with empty string
x = re.sub('[^a-zA-Z]', '', str(x))

#display results
print(x)

ABCDE

Notice that we don’t receive an error because we used str() to first convert the list to a string object.

The result is the original list with each non-letter replaced with a blank.

Note: You can find the complete documentation for the re.sub() function here.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix KeyError in Pandas
How to Fix: ValueError: cannot convert float NaN to integer
How to Fix: ValueError: operands could not be broadcast together with shapes

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 byteslike 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 byteslike 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.

Понравилась статья? Поделить с друзьями:
  • Expected unqualified id before int ошибка
  • Expected to be a reference ошибка
  • Expected sub function or property ошибка
  • Exception processing message 0xc0000006 unexpected parameters ошибка
  • Exception occurs while importing 3d max revit ошибка