headers =
['name'
,'house_tpye'
,'house_mianji'
,'house_gaodu'
,'house_year'
,'house_address'
,'house_monery'
,'house_danjia',]
with
open
("dict.csv"
,'a+'
, newline=
'', encoding=
'utf-8-sig'
)as f:
writer = csv.dictwriter(f, headers)
writer.writeheader(
) datas =
[items]
for row in datas:
writer.writerows(
[row]
)print
("完成---"
)# 寫入表頭
import pandas as pd
df = pd.read_csv(
'tf.csv'
,header=
none
,names=
['a'
,'b'
,'c'
,'d'
,'e'
,'f'
,'g'
,'h'
,'i'
,'j'
,'k'])
df.to_csv(
'tf.csv'
,index=
false
)"""
tf.csv是你當前目錄下的csv檔名
names後面是你想要設定的表頭
切記:header值為none,不能是false,否則會報錯
"""
- writer.writerows(
[row]
) writerows 需要引入列表
- writer.writerow(row) writerow 直接寫入字典
python3 csv資料讀入 寫出
這是讀入1 import csv2 開啟檔案,用with開啟可以不用去特意關閉file了,python3不支援file 開啟檔案,只能用open 3 with open csv r encoding utf 8 as csvfile 4 讀取csv檔案,返回的是迭代型別 5 read csv.rea...
Python3 寫入csv檔案
import json import codecs res res json.dumps res,ensure ascii false res str res f codecs.open read.csv a encoding gbk f.write res f.close 1 使用codecs將包...
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 然後是...