Python TypeError: 'str' does not support the buffer interface

前端 未结 1 1667
我寻月下人不归
我寻月下人不归 2020-12-11 13:52

I have the below code that was working fine and then started throwing this error. I have a csv file that I am trying to write one row to. While other solutions involve conve

相关标签:
1条回答
  • 2020-12-11 14:24

    In Python 3, the csv module expects you to give it a file in text mode:

    with open('data.csv', 'w', newline='') as out:
    

    The newline='' argument gives the csv module control over how newlines are written (the reason why in Python 2 you opened the file in binary mode).

    From the csv.writer() documentation:

    If csvfile is a file object, it should be opened with newline=''.

    [...]

    If newline='' is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline='', since the csv module does its own (universal) newline handling.

    Because you gave the module a binary-mode file, you can only write bytes data to it.

    0 讨论(0)
提交回复
热议问题