Python寫入到csv檔案存在空行的解決方法

2022-08-13 11:24:15 字數 703 閱讀 2753

我在使用python將資料寫入到csv檔案中,發現採用下面的方法,寫入到csv中會存在一行間一行的問題

with open(os.path.join(outpath,'

result.csv

'),'w'

) as cf:

writer =csv.writer(cf)

writer.writerow([

'shader

','file'])

for key , value in

result.items():

writer.writerow([key,value])

為了解決這個問題,查了下資料,發現這是和開啟方式有關,將開啟的方法改為wb,就不存在這個問題了,也就是

在read/write csv 檔案是要以binary的方式進行。

with open(os.path.join(outpath,'

result.csv

'),'wb'

) as cf:

writer =csv.writer(cf)

writer.writerow([

'shader

','file'])

for key , value in

result.items():

writer.writerow([key,value])

python寫入檔案到CSV

編碼問題!使用csv模組時,寫入中文在python中是預設unicode編碼,寫入時csv會出錯,寫不進去資料。import csv with open review.csv ab as f fieldnames comment user time writer csv.dictwriter f,f...

python使用csv寫入csv檔案

沒什麼好說的,直接上 吧 with open file.csv w encoding utf 8 newline as csvfile writer csv.writer csvfile 首先是表頭 writer.writerow id name gender birthday rating 然後是...

python讀取 寫入csv檔案

總是記不住怎麼讀取csv檔案,每次都是上網查,寫個部落格記錄下來看看會不會記得更清楚。個人比較喜歡用pandas的read csv函式來讀取csv檔案 import pandas as pd train data pd.read csv data train.csv 讀取後的資料是dataframe...