I'm stuck with this problem. I followed many examples of code found here on SO an on official documentation pages, but what I get is:
Python 3.7.0 (default, Aug 22 2018, 20:50:05)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>>
>>> float_list = [1.13, 0.25, 3.28]
>>>
>>> with open('some.csv', "wb") as file:
... writer = csv.writer(file, delimiter=',')
... writer.writerow(float_list)
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
TypeError: a bytes-like object is required, not 'str'
>>>
Same thing also with:
int_list=[1,2,3]
Some suggestions?
The "wb" used in your code means that you are writing to the file (using w ) and that you are writing in binary mode (using b ).
For this reason you get the error that you are seeing, you told the writer to expect bytes but then you are sending strings.
Please change:
with open('some.csv', "wb") as file:
To:
with open('some.csv', "w") as file:
Here's more technical details:
On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it'll corrupt binary data like that in JPEG or EXE files.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.