In python2/python3, there will be a blank line when writing csv files and a summary of Solutions to Chinese Encoding Problems,
Copyright Disclaimer: This article is an original article by the blogger. For more information, see.
Running Environment: python2.7.13
Python3.6.0
Windows
IDE: pycharm
Background: When crawling, you need to write the csv format. However, it is uncomfortable to find that all contents in the csv file are empty and show a row,
So I checked the solution on the Internet. In most cases, it was written in 'wb 'mode and found that it is easy to use Chinese characters.
Garbled characters. Later, I learned how to solve the problems of python2 and python3.
The source code is too long. For convenience, the writing content is simplified here. In most cases, errors may occur when the written content contains Chinese characters,
Therefore, the example contains Chinese characters.
(1) In python2
1) In python2, we must 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 using a table, garbled characters are displayed, such:
2) In addition, when using the open () function, you must open it in 'wb 'mode to avoid storing one row in one row.
The complete code is as follows:
# Declare that the global encoding is gbk. If UTF-8 encoding is used, Chinese characters are garbled when the csv file is opened directly in excel.
# Coding: gbkimport csvheaders = ['id', 'username', 'age', 'height'] lines = [('1', '文', '26 ', '20140901'), ('2', 'z文', '27', '123'), ('3', 'Bruce ', '28 ', '200')] with open('wen.csv ', 'wb') as f: # open it in 'wb 'mode. If binary B is not used, A row of f_csv = csv is stored in the same row. writer (f) f_csv.writerow (headers) # Write 1 row (column index) f_csv.writerows (lines) # write multiple rows (data)
(2) python3
In python3, it is easy to do. You only need to write newline = ''In the open () function parameter. By default, newline = None will wrap and write, so there will be a blank line.
The complete code is as follows:
Import csvheaders = ['id', 'username ', 'age', 'height'] lines = [('1', '文', '26 ', '20140901'), ('2', 'z文', '27', '123'), ('3', 'Bruce ', '28 ', '000000')] with open('wen.csv ', 'w', newline = '') as f: # Open and write the attribute newline ='' In 'W' mode '', then, f_csv = csv is not written into each row. writer (f) f_csv.writerow (headers) # Write 1 row (column index) f_csv.writerows (lines) # write multiple rows (data)
OK! Perfect display:
Note: If you want to know why newline = ''is added, there will be no blank line. If you are interested, 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.