Python Learning Note Eight: File manipulation (cont.), file encoding and decoding, functions, recursion, functional programming introduction, high-order functions

Source: Internet
Author: User
Tags readable

File Operations (cont.)

Get file handle position, F.tell (), starting at 0, counting by character Count

F.read (5), read 5 characters

Returns a file handle to a location, F.seek (0)

The file changes the encoding during editing, F.detech ()

Get file Encoding, f.encoding ()

Gets the number of the file in memory, F.fileno ()

Get file terminal type (TTY, printer, etc.), F.isatty ()

Get file name, F.name ()

Determine if a file handle can be moved (TTY, etc.), f.seekable ()

Determine if the file is readable, f.readable ()

Determine if the file is writable, f.writeable ()

Force flush (write file to hard disk), F.flush ()

To demonstrate the Flush method:

Import Sys,time

For I in range (10):

Sys.stdout.write ("#")

Sys.stdout.flush ()

Time.sleep (0.1)

Empty the contents of the file, R,w mode can not be used? F.trancate (), no content fully emptied, otherwise truncated after specifying the number of characters from the beginning

r+, read/write mode, open file reading, append content

w+, write read mode, create file write, only append write, not very useful

A +, Additional Reading

RB, binary file read, for network transfer, binary file use, binary files must be used during network transfer

WB, binary file write, write binary, Str.encode ("Utf-8")

U,ru,r+u, converting \ r \ n to \ n

When the file is processed, the modification of the source file overwrites the content after the modified location, modifying the method:

    1. Loading into memory processing
    2. Put file modifications into a new file

File open, auto Close after operation, with open (file, mode, encode) as F_obj

In code, \ for line wrapping, one line not more than 80 characters, using \ NewLine

Character encoding and decoding

First, you need to know the character set, use decode decoding, and then encode with the target character set, using encode, so that it can be displayed in the target character set of the operating system

Unicode encoding is used by default in Python3 (unlike py file encoding), Python2 is ASCII-encoded by default, Linux is Unicode encoded, Windows GBK encoded

Get python default encoding, sys.getdefaultcoding ()

S=u "Hello", u means Unicode encoding, S.decode ("Utf-8") error

How does GB2312 convert to Utf-8 in Python2?

When a character set is converted, it is transcoded to Unicode, encoded as the target character set

Function

keywords, def

Process, Def, no return value, implicit return none

Import time

Time_format = "%y-%m-%d%x"

Time_current = Time.strftim (Time_format)

def test1 ()

Pass

No return value, None;1 return value, object, multiple return values, tuples

Call Mode:

Keyword call, test (x, y), test (Y=1, x=2), explicit change call order

Position call, corresponding to the parameter one by one

Mixed call, call by location, keyword call cannot be written in front of position parameter

Test (x, Y, z), test (2,z=3,y=6)

Assigning default values to formal parameters, using default parameter values

Scenario: The default setting when installing a program

When arguments are not fixed, parameter groups are used

def test (*args), multiple arguments into tuples

Pass value, test (1,2,3,4,5), test (*[1,2,3,4,5])

Can be combined with fixed parameters, test (X,*args)

Dictionary parameters, def test (**kwargs)

Pass value, test (name= "Alex", age=8,sex= "M")

Pass dictionary, Test (**{"name": "Alex", "Age": 8, "Sex": "M"})

Can be used in conjunction with positional parameters, default parameters, parameter groups

Order of binding: Positional parameters, default parameters, parameter groups, dictionary parameters

Scope and Declaration order

function calls must be behind a function declaration, and the order of declarations between functions is not required.

A local variable takes effect only within a function (procedure), which is the scope of the variable (the range of functions)

Global variables should be defined at the top level

You can declare and modify global variables inside a function (procedure), declare variables using the Global keyword, but do not modify global variables using this method, because a function may be called by parts of the system, which causes the value of the global variable to be indeterminate. If you want to modify a global variable, you can return the modified value in the function.

An object can be modified in a function (procedure), and if you do not want to have such an effect, the cloned object should be used in the function.

Recursive

A function calls itself internally

Characteristics:

There must be a definite end condition, no end condition will result in exceeding the maximum number of recursive layers (English) error, maximum 999 levels

Every step deeper, the problem should be less than it was last time.

Inefficient, function is called by the stack, may cause stack Overflow

In Python, division by default can be the result of a decimal, and if you want to take an integer, truncate (//) with an int (1/2). )

Recursive Plus breakpoint debugging

Function-Type programming

is a mathematical calculation, the input is determined, the output is determined, there is no variable, there is no logical judgment (so that the output can be determined), the Python section supports.

Language: List,haskell,erlang

Higher order functions

One function receives another function as a parameter, and this function is called a higher order function

Python Learning Note Eight: File manipulation (cont.), file encoding and decoding, functions, recursion, functional programming introduction, high-order functions

Related Article

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.