How Python handles garbled text when writing to a CSV file

Source: Internet
Author: User

Objective

Python2 biggest pit lies in Chinese coding problem, encountered Chinese error first add u, then various encode, decode.
When the list, tuple, dict inside the Chinese, printed out is Unicode encoding, this is no solution.
The Chinese code tangled suggestion as soon as possible change Python3 bar, Python2 and use and cherish!

CSV Chinese garbled

1.open Open a CSV file and write the data in Chinese with writer

    • Writer writes a single line
    • Writers writing Multiple lines
# coding:utf-8import csvf = open("xieru.csv", ‘wb‘)writer = csv.writer(f)# 需要写入的信息data = ["客户名称", "行业类型", "客户联系人", "职位", "联系方式", "邮箱","地址"]writer.writerow(data) # 写入单行# writer.writerows(datas) # 写入多行f.close()

2. Open CSV file, find written in Chinese garbled

Encoding and decoding

1. Chinese garbled problem has always been python2 lingering pain, here first to figure out the reason for garbled:

    • Python2 itself code is encoded in STR and Unicode two
    • However, when the file is written to the Windows system, the GB2312 encoding on Windows

So it caused the problem of garbled

2. First decode the Chinese string in Python into Utf-8, then encode to GBK code

Data.decode (' Utf-8 '). Encode (' GBK ')

3. If you are reading a CSV file, in turn:

Data.decode (' GBK '). Encode (' Utf-8 ')

Solution Solutions

1. Scenario One: Encoding the string conversion (this is too cumbersome, not recommended)

 # coding:utf-8import CSVF = open (  "Xieru1.csv",  ' WB ') writer = Csv.writer (f) # information to be written data = [ "Customer Name",  "customer Contact",  "position",  "contact information",  "Mailbox",  "address"]a = []for i in data:a.append (I.decode ( "GBK")) Writer.writerow (a) # Write a single line # writer.writerows (datas) # Write Multiline f.close ()      

2. Method Two: Use the Open method provided by codecs to specify the language encoding of the opened file, which is automatically converted to internal Unicode at read time (recommended)

# Coding:utf-8import csv, Codecsimport sysreload (SYS) sys.setdefaultencoding (' UTF8 ')f = Codecs.open ("Xx.csv",' WB ',"GBK")writer = Csv.writer (f) Writer.writerow (["Customer Name","Industry Type","Customer Contact","Position","Contact Information","Mailbox", "address"]) # Multiple sets of data are stored in the list datas = [[ "Customer Name",  "industry Type",  "customer Contact",  "position",  "contact", ",  "address"], [ "Customer Name",  "Industry type",  "customer Contact",  "Jobs",  "contact",  "Mailbox",  "address"], [ "Customer Name",  "industry type",  "customer Contact",  "Jobs", " contact information,  "Mailbox",  "address"],]writer.writerows (datas) F.close () 


moved from the leisurely place.

How Python handles garbled text when writing to a CSV file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.