python_01 module 1_CSV

1. Description:
https://blog.csdn.net/tcy23456/article/details/85228189
. 1) to read and write default delimiters (DELIMITER) with a comma, double quote as reference character (quotechar)
2) with the write data writer None repr written is called an empty string, float () is converted into a string. Non-data string str () is stored as a string.
3) open function

Import the locale; locale.getpreferredencoding () # view local coding 'cp936' 
Open ( ' some.csv ' , NEWLINE = '' , encoding = ' UTF-. 8 ' )
 # using the system default encoding the decoded files may use different unicode codec file 
# If newline = '' is not specified, a reference line break will not be fitted correctly interpreted within the field

 


4) RFC 4180 provides standard:
# plain text content; contains records
each record # is separated by a single character attributes each partition
# attribute record sequence of each is the same as pandas can be installed PyPI the pip. 
2. Module content 

2.1 Module constant: Conventions reference

quoting = csv.QUOTE_MINIMAL = 0 # writer objects only those references that contain special characters
quoting = csv.QUOTE_ALL = 1 # writer object reference for all fields
# as field separators, quotechar or any character lineterminator .
quoting = csv.QUOTE_NONNUMERIC = 2 # writer object references of all non-numeric fields.
# Instruct the reader will convert all non-reference field to float.
quoting = csv.QUOTE_NONE = 3, escapechar = '$' # writer object does not reference field
# EscapeChar error is thrown if not set; quote characters indicate reader does not perform special processing. 
2.21 function:.

Csv.reader (F, dialect = 'Excel', ** fmtparams) # returns a read object, will iterate to csv file Yukisada.
# Properties:
csv.reader () dialect.
Csv.reader () line_num.

csv.writer (f, dialect = 'excel ', ** fmtparams) # Returns a writer object, the user is responsible to convert the data to a given partition string on file-like object
# property methods:
writer.writerow
writer.writerows
writer.dialect

csv.DictReader () read dictionary #
# method:
csv.DictReader () Next .__ __ () call # Next (Reader)

# properties:
csvreader.dialect dialect # parser read-only use described.
csvreader.line_num # number of rows read from the source iterator. This is a different number of records returned, because the records can span multiple lines.
csvreader.fieldnames # read from the first record in the initialization file of this property

csv.DictWriter ()
# Method:
csvwriter.writerow (row) to row # writer to write the target file, formatted according to the current dialect. Support iteration
csvwriter.writerows (rows) # line all the elements written to the file object writer and formatted according to the current dialect. Support iteration
DictWriter.writeheader () # public methods: write a single line with a field name

# property:
csvwriter.dialect # dialect used by read-only DESCRIPTION 
2.22 function - dialect 

csv.register_dialect (name [, dialect [, ** fmtparams]])
# The name linked with the dialect. name must be a string.
Dialect # Dialect by passing a subclass of the keyword or parameter, or both fmtparams specified keyword parameter override parameters and dialect.

csv.unregister_dialect (name)
# removed from the dialect registry with the name associated dialect. Name throw an error does not exist

csv.get_dialect (name)
# returns associated with the name of the dialect, immutable Dialect. Name does not exist throw an error.

csv.list_dialects ()
# returns the names of all registered dialects.

csv.field_size_limit ([NEW_LIMIT])
# parser returns the current allowed maximum field size. If given new_limit, then this will become the new restrictions. 
2.23 categories:

csv.Dialect class
# is used to define a container class instance of a writer or reader specific parameters.

csv.excel class
# excel in the class definition of the nature generally Excel generated CSV file. It registered dialect name 'excel'.

class csv.excel_tab
generally defined nature # Excel generated tab-delimited file. It registered dialect name 'excel-tab'.

csv.unix_dialect class
# define the CSV on a UNIX system, i.e., using the generated file '\ n' as the line terminals and all the fields referenced. It Dialect Register to name 'UNIX'

class csv.Sniffer
# Sniffer CSV file format for deriving classes.

Sniffer class method:
sniff (the Sample, required delimiter and = None)
# analysis of a given sample and return parameters Dialect reflects found subclasses.
# If the optional parameters of the delimiters, it is interpreted as a valid separator may comprise a string.

has_header (Sample)
# sample text analysis (assuming CSV format), True if the first line is displayed in a series of column headings, it is returned. 
Example 3.1: string read

for row in csv.reader(['one,two,three']):
print(row)# ['one', 'two', 'three'] 
3.2实例:读写 

Example 1.1: The simplest example of reading CSV file:
with Open ( 'some.csv', NEWLINE = '') AS F:
Reader = csv.reader (F)
for Row in Reader: Print (Row)

Example 1.2: corresponding the simplest example is written:
Import CSV
with Open ( 'some.csv', 'W', NEWLINE = '') AS F:
Writer = csv.writer (F)
writer.writerows (someiterable)

example 2.1: write data
Open with ( 'test_csv_data.csv', 'W', NEWLINE = '') AS F:
Writer = csv.writer (F, DELIMITER = '', quotechar = '|', quoting = csv.QUOTE_MINIMAL)
writer.writerow ( [ 'My name IS', 'Tom', 'Bob', 'Jim', 'On May'])
writer.writerow ([ 'Color IS', 'Red', 'Yellow Green', 'Blue'])

example 2.2 : read data
with open ( 'test_csv_data.csv', newline='') as f:
spamreader = csv.reader(f, delimiter=' ', quotechar='|')
Row in spamreader for:
Print ( ',' .join (Row))

# My name IS, Tom, Bob, Jim, May
# IS Color, Red, Yellow Green, Blue

instance 3.2: Writing a csv file

with open ( 'csv_test .csv ',' w ', newline =' ') as f: # is not specified as newline =' ', and sometimes written to each line will be written to a blank line
Writer = csv.writer (F)
writer.writerow ( [ 'name', 'age' , 'tel']) # line is written with WriteRow

Data = [( 'Tom', '25', '1367890900'), ( 'Jim', '18 is', '1367890800') ]
writer.writerows (Data) with a plurality of rows # writerows

example 3.2: reading
with Open ( 'csv_test.csv', encoding = 'UTF-. 8') AS F:
csv_reader = csv.reader (F)
for row in csv_reader:
Print (Row)

# [ 'name', 'Age', 'Tel']
# [ 'Tom', '25', '1,367,890,900']
# [ 'Jim', '18 is', '1367890800'] 
3.3 Dictionary reader

# Example 1: dictionary mode write
datas = [{ 'name': 'Bob', 'age': 23}, { 'name': 'Jerry', 'age': 44}, { 'name': 'Tom' , 'Age': 15}]

with Open ( 'test_csv_data.csv', 'W', NEWLINE = '') AS F:
Writer = csv.DictWriter (F, [ 'name', 'Age']) # header here passed as the first row data
writer.writeheader ()
for DATAS in row:
writer.writerow (row)
# may also be written to multiple rows
writer.writerows (DATAS)

# example 2: read dictionary embodiment
Import CSV
with Open ( 'test_csv_data.csv', 'R & lt') AS F:
Reader = csv.DictReader (F)
for Row in Reader:
Print (Row [ 'name'], Row [ 'Age'])

# name, Age
# Bob, 23
# Jerry, 44
# Tom,15
# Bob, 23
# Jerry, 44
# Tom, 15 
3.4csv file format

Method # 1: The definition of a subclass csv.Dialect (e.g., special delimiter string reference convention line terminator, etc.):

class my_dialect (csv.Dialect):
LineTerminator = '\ n-'
DELIMITER = ';'
quotechar = ' "'

. csv = Reader Reader (F, diaect = my_dialect)

# 2: parameter csv each method provided in the form of a keyword sv.reader C:

Reader csv.reader = (F, DELIMITER = '|' ) 
3.5 register a new dialect: 

csv.register_dialect ( 'unixpwd', DELIMITER = ':', quoting = csv.QUOTE_NONE)
with Open ( 'the passwd', NEWLINE = '') AS F:
Reader csv.reader = (F, ' unixpwd ') 
3.6 to capture and report errors: 

Import CSV, SYS
filename =' some.csv '
with Open (filename, NEWLINE =' ') AS f:
Reader = csv.reader (f)
the try:
for Row in Reader:
print(row)
except csv.Error as e:
sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e)) 
3.7示例Sniffer: 

with open('test_csv_data.csv', newline='') as f:
dialect = csv.Sniffer().sniff(f.read(1024))
f.seek(0)
reader = csv.reader(f, dialect,delimiter=' ',quotechar='|', quoting=csv.QUOTE_NONE,escapechar='$')
# process CSV file contents here
for i in reader:
print(i)

# ['name is', 'age', 'weight', 'remark']
# ['Tom', '25', '31.2', '$te|st']
# ['Jim', '35', '42.8', '$test$st'] 
3.8实例:dialect 

Open = F ( 'test_csv_data.csv', NEWLINE = '')
spamreader csv.reader = (F, DELIMITER = '', quotechar = '|')
spamreader.dialect, spamreader.line_num

dialect = spamreader.dialect

dialect.delimiter # separate the fields single character string # ''
dialect.doublequote # how to handle the fields within the reference symbols. If True, the double writing. 1 #
dialect.escapechar # string used to escape the separator disabled = None #
dialect.lineterminator # write operation for line endings # '\ R & lt \ n-'
dialect.quotechar # fields with reference to a special character (e.g., separator) of the symbol # '|'
dialect.quoting agreed reference # 0 #
dialect.skipinitialspace # ignoring delimiters back whitespace. The default is 0 # False
dialect.strict # How to deal with a reference symbol # 0 in field 
3.9 Example: - quote Conventions 





















= csv.QUOTE_NONNUMERIC quoting, EscapeChar = None
# writer object reference indicating all non-numeric fields. Instruct the reader to convert all non-reference field to float.

# | Name IS | | Age | | weight | | Use the remark |
# | Tom | 25 31.2 | $ TE || ST |
# | Jim | 35 42.8 | $ $ the Test ST |

quoting = csv.QUOTE_NONE, EscapeChar = '$'
# indicates the writer does not refer to the object field. When this occurs the output data separator, which is in front of the current character escapechar.
# If no error is thrown escapechar; indicates the reader does not quote characters to perform special processing.

Age weight name IS $ # Use the remark
# 25 Tom $$ 31.2 $ TE | ST
# 35 Jim $$ 42.8 $$ the Test ST 
4. Remarks 

Parameter Description
single-character string delimiter used to separate fields. The default is ","
LineTerminator writes for line endings, the default is " '\ r \ n'. Read this option is ignored, it can recognize the line of cross-platform endings
quotechar used with special characters ( the separator reference) symbol fields default to ' "'
quoting reference conventions. Possible values are csv.QUOTE _ ALL (cited in all fields)
  csv.QUOTE_MINIMAL (reference field separators such as special characters) default
  csv.QUOTE_NONNUMERIC 
  csv.QUOTE_NON (not referenced)
skipinitialspace ignoring delimiters back whitespace. The default is False
doublequote how to handle the reference symbols within the field. If True, the double write.
escapechar string delimiter for escape (e.g. quoting = csv.QUOTE_NONE disabled by default
---------------------
OF: tcy23456
Source: CSDN
Original: https://blog.csdn.net/tcy23456/article/details/85228189
Disclaimer: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/taysem/p/10963142.html