Home  >  Q&A  >  body text

python csv模块生成CSV文件,0字头数字缺失,汉字乱码

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字头,汉字乱码

天蓬老师天蓬老师2742 days ago946

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:52:05

    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 the utf8 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 for utf8 files with BOM headers)gbk),它无法自动识别无BOM格式的utf8格式,除非在文件前面加上3字符的BOM头。(在 office 2007 下的某个版本,对有BOM头的utf8文件也有Bug)

    你应该使用文本编辑器(不要用notepad)打开看看里面的内容。


    补充说明下:
    微软家的老牌软件对于utf8无bom文件的支持都很弱。bom头是utf8文件开头加上三个字符(十六进制是EFBBBF),具体可以搜索utf8 bom头
    没有太完美的方案,我列出来你根据情况选择一下:

    1. 如果想保持 utf 编码,可以尝试在文件前加上 Bom 头。(就是写入文件前先写入三字符)

    2. 如果只是给 Excel 使用,并且在大陆使用,你可以直接把文件写成 GBK

      You should use a text editor (not notepad) to open it and see the contents.

    Additional explanation: 🎜Microsoft’s old software has weak support for UTF8 BOM-less files. The bom header is the beginning of the utf8 file plus three characters (hexadecimal is EFBBBF). For details, you can search for utf8 bom header. 🎜There is no perfect solution, I will list it for you to choose according to the situation: 🎜
    1. 🎜If you want to keep the utf encoding, you can try adding the Bom header in front of the file. (That is, write three characters before writing the file)🎜🎜
    2. 🎜If it is only for Excel and used in mainland China, you can directly write the file into GBK encoding. 🎜🎜 🎜

      reply
      0
  • 天蓬老师

    天蓬老师2017-04-17 17:52:05

    The results of my test:

    id,url,keywords
    0011,http://www.59store.com/,59store.com
    0022,http://59data.top/,59data.top
    0033,http://my.space.zmx/,汉子乱码?

    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 and OpenOffice Calc both have this phenomenon) NumbersOpenOffice Calc 都有這個現象)

    比如說,id 欄位的型態如果設為數字,則前面不必要的 0 可能會自動被忽略. 像這一點可以試試看改成純文字型態再開啟.

    P.S. ExcelFor 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 Excel part can be found in the Yuhe CC instructions.

    🎜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). 🎜

    reply
    0
  • Cancelreply