#-*- coding: utf-8 -*-
import
csvimport
osdef
writetocsv():
'''寫csv檔案
'''titls = ['
序號', '
鏈結', '備註'
] data =[
['1', '
', '小黑'
], ['2
', '
', ''],
['3', '
', '京東'
] ]
csvfile = os.path.join(os.getcwd(),'
csvtest.csv')
with open(csvfile, 'wb
') as f:
writer =csv.writer(f)
#標題writer.writerow(titls)
#內容writer.writerows(data)
defreadcsv():
'''讀取csv檔案
'''filename = os.path.join(os.getcwd(), '
csvtest.csv')
ifos.path.exists(filename):
with open(filename, 'r
') as f:
reader =csv.reader(f)
for item in
reader:
item
#writetocsv()
#readcsv()
上述**,開啟csv檔案,現在正常,但是在別人機器上開啟是亂碼,後來在網上查下,下加兩行**就了
首先引入
import codecs然後增加
f.write(codecs.bom_utf8)完整**如下:
#-*- coding: utf-8 -*-
import
csvimport
osimport
codecs
defwritetocsv():
'''寫csv檔案
'''titls = ['
序號', '
鏈結', '備註'
] data =[
['1', '
', '小黑'
], ['2
', '
', ''],
['3', '
', '京東'
] ]
csvfile = os.path.join(os.getcwd(),'
csvtest.csv')
with open(csvfile, 'wb
') as f:
f.write(codecs.bom_utf8)
writer =csv.writer(f)
#標題writer.writerow(titls)
#內容writer.writerows(data)
defreadcsv():
'''讀取csv檔案
'''filename = os.path.join(os.getcwd(), '
./data/20170708.csv')
ifos.path.exists(filename):
with open(filename, 'r
') as f:
reader =csv.reader(f)
for item in
reader:
item
#writetocsv()
#readcsv()
Python讀寫csv檔案
1.寫入並生成csv檔案 coding utf 8 import csv csvfile file csv test.csv wb writer csv.writer csvfile writer.writerow 姓名 年齡 data 小河 25 1234567 小芳 18 789456 writ...
python 讀寫csv檔案
1.忽略第一行標題的基礎上 python2.7 coding utf 8 import csv csv reader csv.reader open r c users thinkpad desktop tweets.csv for row in csv reader 條件語句忽略第一行檔案資料 i...
python 讀寫csv檔案
1.將dataframe資料寫入csv 1 用 csv包一行一行的寫入 import csv python2可以用file替代open with open test.csv w as csvfile writer csv.writer csvfile 先寫入columns name writer.w...