久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何以二進制模式編寫csv文件?

How to write a csv file in binary mode?(如何以二進制模式編寫csv文件?)
本文介紹了如何以二進制模式編寫csv文件?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

python 的 csv writer 不再支持二進制模式了嗎?

Does python's csv writer not support binary mode anymore?

直到現在我才不得不在 'b' 模式下寫入,但我遇到了非常煩人的錯誤,如下所示:

I haven't had to write in 'b' mode until now and i'm getting very annoying errors like so:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-030fb0c9dc9a> in <module>()
  4 with open('test.csv', 'wb') as f:
  5     w = csv.writer(f)
----> 6     w.writerows(rows)

TypeError: a bytes-like object is required, not 'str'

代碼:

import csv

rows = [b'1,2,3', b'4,5,6', b'7,8,9']
with open('test.csv', 'wb') as f:
    w = csv.writer(f)
    w.writerows(rows)

如果有人能解釋錯誤,那就太好了.我傳入了一個迭代,其中每個元素都是一個字節序列,但我仍然收到一個錯誤,即輸入不是字節"而是str".這種行為似乎出乎意料.

If anyone could explain the error that would be great. I'm passing in an iterable where every element is a byte sequence but I still get an error about the input not being 'bytes' but instead being 'str.' This behavior seems unexpected.

我知道如果我關閉二進制模式,上面的代碼片段可以寫入普通文件.如果有人有建設性的解決方案或建議,我將非常感激.

I know the above code snippet can write to a normal file if I turn off the binary mode. If anyone has a solution or suggestion that is constructive I would very much appreciate it.

推薦答案

Python 3 中的 csv 模塊總是嘗試寫入字符串,而不是字節:

The csv module in Python 3 always attempts to write strings, not bytes:

為了盡可能輕松地與實現 DB API 的模塊進行交互,值 None 被寫為空字符串.[...] 所有其他非字符串數據在寫入之前都使用 str() 進行字符串化.

To make it as easy as possible to interface with modules which implement the DB API, the value None is written as the empty string. [...] All other non-string data are stringified with str() before being written.

這意味著你必須向它傳遞一個接受字符串的文件對象,這通常意味著以文本模式打開它.

That means you have to pass it a file object that accepts strings, which usually means opening it in text mode.

如果您遇到需要字節的文件對象,請將其包裝在 io.TextIOWrapper 處理 str->bytes 編碼:

If you are stuck with a file object that wants bytes, wrap it in an io.TextIOWrapper to handle str->bytes encoding:

# assuming you want utf-8
with io.TextIOWrapper(binary_file, encoding='utf-8', newline='') as text_file:
    w = csv.writer(text_file)

這篇關于如何以二進制模式編寫csv文件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 91精品国产综合久久福利软件 | 国产乱码精品一区二区三区中文 | 久久久精品综合 | 国产成人综合网 | 精品欧美一区二区在线观看欧美熟 | 激情视频中文字幕 | 日韩久久久一区二区 | 精品一区二区三区在线观看国产 | 中文字幕国产一区 | 风间由美一区二区三区在线观看 | 欧美一区视频在线 | 国产精品视频网 | 亚洲国产专区 | 国产精品成av人在线视午夜片 | 欧美日本一区 | 免费观看a级毛片在线播放 黄网站免费入口 | 国产一二三区精品视频 | 欧美做暖暖视频 | 欧美五月婷婷 | 国产草草视频 | 国产精品视频97 | 狠狠躁天天躁夜夜躁婷婷老牛影视 | 国精久久 | 国产精品亚洲精品久久 | 日本不卡一区二区三区在线观看 | 国产成人免费视频网站高清观看视频 | 狠狠狠色丁香婷婷综合久久五月 | 欧美vide| 操人视频在线观看 | 天天插天天操 | 一区二区三区成人 | 久久精品亚洲 | 日韩成人在线视频 | 国产精品婷婷 | 中文字幕国产日韩 | 又黄又爽的网站 | 日韩一及片 | 国产一区二区三区在线免费 | 毛片韩国 | 欧美日韩国产三级 | 国产精品免费一区二区三区四区 |