CSV (Comma-Separated Values file format, sometimes called character-separated values, because the separating character can also be anything other than a comma), whose files store tabular data (numbers and text) in plain text.
CSV is a general, relatively simple file format that can be opened with Notepad, Excel, etc.
1> Plain text, using a character set such as ASCII, Unicode, EBCDIC or GB2312;
2> Consists of records (typically one record per line);
3> Each record is separated into fields by a delimiter (typical delimiter There are commas, semicolons, or tabs; sometimes delimiters can include optional spaces);
4> Each record has the same sequence of fields.
# encoding: utf-8 import csv from time import sleep class Csv_Operator( object ): def __init__ ( self ): """You can pass in the initial parameter file name or path """ pass def csv_write ( self ): arr = [ 'abc' , '123' , ' hahaha ' ] #Array arr2 = [[ 132 , 2543 , 323 ] , [ ' often ' , 'big' , 'center' ] , [ '[email protected] ' , 'hello #comment' , '%s%d' ]] # two-dimensional array tup = (( 'fg' , 'nice' , 'home' ) , ( 1111 ,2222, 3333 ) , ( '!2123' , '@369.email.cn' , '#4444' )) # Yuanzu dic = { 'name' : '123Admin' , 'age' : 23 , 'address' : ' Nanjing Road 128 ' } #dictionary csvfile = open ( 'D: \\ test.csv ' , 'wb+' ) # python2.x uses 'b' to remove blank lines, python3.x uses the newline attribute to remove blank lines writer = csv.writer(csvfile) writer.writerow(arr) writer.writerows(arr2) writer.writerows(tup) writer.writerow([key for key in dic]) writer.writerow([dic[key] for key in dic]) csvfile.close() def csv_read(self): csvfile = open ( 'D: \\ test.csv' , 'rb' ) #read file content reader = csv.reader(csvfile) for line in reader: if line: print line csvfile.close() if __name__ == '__main__': csvnew = Csv_Operator() csvnew.csv_write() print 'end'
I saw that it would be garbled on the Internet, but it didn't appear on my side, so I didn't write a solution.