Я застрял с этой проблемой. Я следил за многими примерами кода, найденными здесь, на SO, на страницах официальной документации, но я получаю следующее:
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'
>>>
То же самое и с:
int_list=[1,2,3]
Некоторые предложения?
«wb», используемый в вашем коде, означает, что вы пишете в файл (используя ж) и что вы пишете в двоичном режиме (используя б).
По этой причине вы получаете ошибку, которую вы видите, вы сказали писателю ожидать байтов, но затем вы отправляете строки.
Пожалуйста измените:
with open('some.csv', "wb") as file:
К:
with open('some.csv', "w") as file:
Вот еще технические подробности:
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.