python CSV模块 写入CSV文件时,0开头的数字会丢失
# _*_ coding:utf-8 _*_
#win7+python2.7.x
import csv
csvfile = file('csvtest.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerow(['id', 'url', 'keywords'])
data = [
('0011', 'http://www.59store.com/', '59store.com'),
('0022', 'http://59data.top/', '59data.top'),
('0033', 'http://my.space.zmx/', '汉子乱码?')
]
writer.writerows(data)
csvfile.close()
写入CSV时会丢失0字头,汉字乱码
It’s an Excel problem.
Excel will guess whether it is a numeric column. If it is a numeric column, it will be displayed numerically by default (0 is omitted).
The default format of Excel is the local character set (the simplified Chinese version is
gbk
). It cannot automatically recognize theutf8
format without BOM format unless 3 characters are added in front of the file. BOM header. (In a certain version under office 2007, there is also a bug forutf8
files with BOM headers)gbk
),它无法自动识别无BOM格式的utf8
格式,除非在文件前面加上3字符的BOM头。(在 office 2007 下的某个版本,对有BOM头的utf8
文件也有Bug)你应该使用文本编辑器(不要用
notepad
)打开看看里面的内容。补充说明下:
微软家的老牌软件对于utf8无bom文件的支持都很弱。bom头是utf8文件开头加上三个字符(十六进制是
EFBBBF
),具体可以搜索utf8 bom头
。没有太完美的方案,我列出来你根据情况选择一下:
如果想保持
utf
编码,可以尝试在文件前加上Bom
头。(就是写入文件前先写入三字符)如果只是给 Excel 使用,并且在大陆使用,你可以直接把文件写成
You should use a text editor (notGBK
notepad
) to open it and see the contents.EFBBBF
). For details, you can search forutf8 bom header
. 🎜There is no perfect solution, I will list it for you to choose according to the situation: 🎜utf
encoding, you can try adding theBom
header in front of the file. (That is, write three characters before writing the file)🎜🎜GBK
encoding. 🎜🎜 🎜The results of my test:
It seems that there is nothing wrong with the plain text file. I guess it may be caused by the spreadsheet software that you use to open the csv file. (Mac's
Numbers
andOpenOffice Calc
both have this phenomenon)Numbers
和OpenOffice Calc
都有這個現象)比如說,id 欄位的型態如果設為數字,則前面不必要的 0 可能會自動被忽略. 像這一點可以試試看改成純文字型態再開啟.
P.S.
Excel
For example, if the type of the id field is set to a number, the unnecessary 0 in front may be automatically ignored. Like this, you can try changing it to plain text and then turning it on.P.S. The
🎜To process xlsx files more accurately and in detail, you can use XlsxWriter, which can control the data type (data type) and even the format (format). 🎜Excel
part can be found in the Yuhe CC instructions.