4.1 Encoding and decoding
4.2 File operations
8th Day of self-study, title arranging study fourth day
Encode encoding
Docode Decoding
Python2 default ASCII format
#-*-Coding:utf-8-*-# What is the code format for declaring a program
# decode text into Unicode format
sname = Name.decode ("Utf-8") # must first declare what the decoded format is, such as Utf-8print (sname) ssname = Sname.encode ("GBK") # encode text into GBK format print (ssname)
# It must be noted that when GBK turns into Utf-8, it must first be converted to Unicode and then to Uft-8, and vice versa.
Python3 default Unicode
b = byte = byte type = 2 binary
Py3 and Py2 Coding differences
Encode the data into the bytes type while coding
Decode the bytes type into a string while decoding
The method that can be called must be an object
File operations, the following methods are used in the file Operation object
R: Read mode operation
W: Write Action Object
A: Append add
r+: Read- write mode when you use write, the text is appended to the end of the last line
w+: Write mode when you use write, the contents of the file are emptied before being written to the file
A +: Additional Reading
Open (1)# on, 1 means one character is printed only
Close# closing files
Write# writing mode
RA = open (' Test file ', ' W ', encoding= ' utf-8 ')
Ra.write ("is the first line to test the string.")
Print (RA)
Ra.close ()
Print Result: <_io. Textiowrapper name= ' test file ' mode= ' W ' encoding= ' Utf-8 ' >
RA = open (' Test file ', ' R ', encoding= ' utf-8 ')
rar = Ra.read ()
Print (RAR)
Ra.close ()
Print Result: Is the first line to test the string.
RAA = open (' Test file ', ' a ', encoding= ' utf-8 ')
Raa.write (' is the second line test string ')
Raa.close ()
Hit this text result: is the first line to test the string. That's the second line. The test string is not wrapped by default, and the default insertion is at the cursor
RAA = open (' Test file ', ' a ', encoding= ' utf-8 ')
Raas = Raa.write (' \ n is the second line test string \ n ')
Raa.close ()
Unlike the previous one, it wraps after the line break
File Operation Flow:
1, open the file, get the file handle and assign value to a variable;
Handle: The Action object of the file
2, through the handle to the file operation;
3. Close the file
ReadLine () Print line default first row
ReadLines () Print all Lines
# Open a test file in a read-only manner, formatted as Utf-8file = open (' Test file ', ' R ', encoding= ' Utf-8 ') # Assign a file variable a = File.readlines () # set an initial value nubmer=0# Loop to print the value of the A-file variable, starting with the first for I in a:# initial value every time you add 1 to 2, use join to change the configuration item to this line, do not print the/n newline character nubmer+=1 if Nubmer = = 2:i = ". Join ([ I.strip (), ' AAAAA ') # Every time this line is printed, when the 2nd time equals join then output the value of the 2nd time variable print (I.strip ()) # Close the file Operation properties, A.close ()
Modify a file
# define two variables and files
F_read = open (' Test file ', ' R ', encoding= ' utf-8 ')
F_write = open (' Test file 2 ', ' W ', encoding= ' utf-8 ')
Number = 0# Sets the variable to 0 loops for a file line, each time adding 1for f in F_read:number + = 1 if numbers = = 2:# When 2 o'clock, assign a f= ' Xiong test to the file variable string. \ n ' F_write.write (f) # finally write it to a new file, you can complete the change file operation
# to manage a single object, this method no longer needs to use the close () method
With open (' Test file ', ' R ', encoding= ' utf-8 ') as F1:
Pass
# Manage Multiple objects
With open (' Test file ', ' R ', encoding= ' Utf-8 ') as F1,open (' Test file 2 ', ' W ', encoding= ' Utf-8 ') as F2:
Pass
Tell detect cursor Position
Print (File.read (4))
Print (File.tell ())
Print Result: is the first
3 characters in Chinese, uft8 format
The file object. Seek () adjust the cursor position
Print (File.seek (1))
Print (File.tell ())
Printed results: 1
1
Flush: Data is removed from the cache and stored synchronously with the disk
Import Sys,time
Sys.stdout.write ("*")
# truncation is done in write mode, must use Write,append format
Truncate # empty file contents
Fileno# File descriptor
Isatty# to see if it's an end device
This article is from the "Xiong" blog, make sure to keep this source http://xiong51.blog.51cto.com/5239058/1964168
Fourth day of basic Python learning