close

[Solved] Type: encoding or errors without a string argument

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Type: encoding or s without a string argument in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Type: encoding or errors without a string argument Error Occurs?

Today I get the following error Type: encoding or s without a string argument in python.

How To Solve Type: encoding or errors without a string argument Error ?

  1. How To Solve Type: encoding or errors without a string argument Error ?

    To Solve Type: encoding or errors without a string argument Error It seems your datas is already in bytes format, so to turn it into UTF-8 strings, you have to use str, not bytes!

  2. Type: encoding or errors without a string argument

    To Solve Type: encoding or errors without a string argument Error It seems your datas is already in bytes format, so to turn it into UTF-8 strings, you have to use str, not bytes!

Solution 1

It seems your datas is already in bytes format, so to turn it into UTF-8 strings, you have to use str, not bytes! Also, you have to convert each element from datas individually, not the entire list at once. Finally, if you want to add datas as one row to out.csv, you have to use writerow, whereas writerows would write all the rows at once, and accordinly would expect a list of lists.

Depending on your OS, you might also have to specify the encoding when opening the file. Otherwise it will use the OS’ default encoding, which might be something entirely different.

This seems to do what you want. The result is a CSV file with one row1 of data in UTF-8 format, and the \xc3\x97 is decoded to ×.

import csv
with open(r"out.csv", "w", encoding='UTF-8') as w:
    writer = csv.writer(w)
    writer.writerow([str(d, 'UTF-8') for d in datas])

1) Note that the last item in datas contains some line breaks, and thus will be split onto several lines. This is probably not what you want. Or is this a glitch in your datas list?

Solution 2

This error just means the thing you’re passing to bytes (the string you want converted to a byte sequence) is not in fact a string. It does not specifically mean that the argument is already of type bytes, just that it isn’t a string.

>>> bytes(b"", encoding="utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: encoding without a string argument
>>> bytes(None, encoding="utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: encoding without a string argument
>>> bytes(12, encoding="utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: encoding without a string argument

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read