How to solve Chinese garbled problem when Python reads and writes CSV

Source: Internet
Author: User

Reference 1

Reference 2

CSV is an abbreviation of the English comma separate values (comma separated value), as the name implies, the contents of the document are composed of "," delimited columns of data, which can be opened using Excel and a text editor. CSV document is an easy-to-edit, visually stunning way to store data

1, Python read-write, append csv method:

' R ': Read-only (default. Throws an error if the file does not exist)
' W ': Write-only (if the file does not exist, the file is created automatically)
' A ': Append to end of file (automatically create file if file does not exist)
' r+ ': Read and write (throws an error if the file does not exist)

1 ImportCsv,os2 ifOs.path.isfile ('Test.csv'):3With open ("Test.csv","R") as CSVFile:4Reader =Csv.reader (csvfile)5         #There's no need for ReadLines .6          forLineinchReader:7             PrintLine
ImportCSV#Python2 can replace open with file#does not exist The file is createdWith open ("Test.csv","W") as Csvfile:writer=Csv.writer (csvfile)#Write Columns_name FirstWriter.writerow (["Index","A_name","B_name"])    #write multiple lines with writerowsWriter.writerows ([[0,1,3],[1,2,3],[2,3,4]])
ImportCSV#Python2 can replace open with file#does not exist The file is createdWith open ("Test.csv","a") as Csvfile:writer=Csv.writer (csvfile)#Write Columns_name FirstWriter.writerow (["Index","A_name","B_name"])    #write multiple lines with writerowsWriter.writerows ([[0,1,3],[1,2,3],[2,3,4]])

2, Excel open CSV file, can identify the code "GB2312", but do not recognize "Utf-8", the database string encoding is utf-8. therefore:

When reading data from CSV to the database, you need to convert the GB2312 to Unicode encoding before converting the Unicode encoding to the UTF-8 encoding: Data.decode (' GB2312 '). Encode (' Utf-8 ')

When reading data from a database to a CSV file, you need to convert the Utf-8 encoding to Unicode encoding before converting the Unicode encoding to the GB2312 encoding: Data.decode (' Utf-8 '). Encode (' GB2312 ‘)

3, decode (' utf-8 ') means to convert UTF-8 encoding to Unicode encoding; encode (' utf-8 ') means converting Unicode encoding to UTF-8 encoding

4, Unicode is just a set of symbols, it specifies the binary code of the symbol, but does not specify how the binary code stored

5, you can use the Python encoding conversion module:codecs

How to solve Chinese garbled problem when Python reads and writes CSV

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.