Python 2 / Python 3 summary of solutions to the problem of empty line and Chinese encoding when writing csv files

Keywords: Python encoding Windows Pycharm

Copyright Statement: This article is the original article of the blogger, reproduced please indicate.

 

Running environment: Python 2.7.13

          python3.6.0

       windows

   IDE: pycharm

Background: When crawling, you need to write in csv format, but find that the contents of the csv file are empty and display one line, which is very uncomfortable.

So I checked the solutions on the internet. In most cases, it means that it's easy to write in the way of'wb'in Chinese.

Out of the code. Later, after a lot of twists and turns, we summarized the different solutions under Python 2 and python 3.

 

The source code is too long. For convenience of explanation, the writing content is simplified here. Because in most cases, it is easy to make mistakes when writing content containing Chinese.

So examples will contain Chinese.

(1) In the case of Python 2

1) In the case of Python 2, we first declare the global encoding. Here we use gbk instead of utf8.
utf8 can also be written, but if you open the csv file directly with a table, you will see the scramble code as follows:

2) When using the open() function, we should open it in the way of `wb', which can avoid the situation of storing one row in another row.

The complete code is as follows:

 

#Declare global encoding as gbk,If used utf8 Coding is used directly excel open csv When it comes to Chinese, it will display scrambled code.
#
coding:gbk import csv headers = ['ID','User name','Age','height'] lines = [('1','Wen Zi','26','175'), ('2','Ancestors','27','185'), ('3','Bruce','28','195')] with open('wen.csv','wb') as f:#Here to'wb'Open in a binary mode b,It stores one row separately f_csv = csv.writer(f) f_csv.writerow(headers)#Write 1 row (column index) f_csv.writerows(lines)#Write multiple lines (data)

 

(2) In the case of Python 3

In the case of Python 3, it's easy to do, just write newline =''in the parameter of open() function. By default, newline=None will be written in a new line, so it will be empty.

The complete code is as follows:

 

import csv

headers = ['ID','User name','Age','height']
lines = [('1','Wen Zi','26','175'),
         ('2','Ancestors','27','185'),
         ('3','Bruce','28','195')]

with open('wen.csv','w',newline='') as f:#with'w'Way to open and write attributes newline='',It will not be written every other line.
    f_csv = csv.writer(f)
    f_csv.writerow(headers)#Write 1 row (column index)
    f_csv.writerows(lines)#Write multiple lines (data)

OK! Perfect display:

 

 

Attachment: If you want to know why you add newline=', you won't leave a blank line. Interested comrades can refer to the source code description (On input is written):

newline controls how universal newlines works (it only applies to text
    mode). It can be None, '', '\n', '\r', and '\r\n'.  It works as
    follows:
    
    * On input, if newline is None, universal newlines mode is
      enabled. Lines in the input can end in '\n', '\r', or '\r\n', and
      these are translated into '\n' before being returned to the
      caller. If it is '', universal newline mode is enabled, but line
      endings are returned to the caller untranslated. If it has any of
      the other legal values, input lines are only terminated by the given
      string, and the line ending is returned to the caller untranslated.
    
    * On output, if newline is None, any '\n' characters written are
      translated to the system default line separator, os.linesep. If
      newline is '' or '\n', no translation takes place. If newline is any
      of the other legal values, any '\n' characters written are translated
      to the given string.

Posted by jblallement on Tue, 04 Jun 2019 12:49:52 -0700