Python Introductory article (v) file manipulation and character encoding

Source: Internet
Author: User

1, file operation 1, file operation Flow:
打开文件,得到文件句柄并赋值给一个变量===>  file = open("yesterday",encoding="utf-8")通过句柄对文件进行操作关闭文件 ==>  file.close()
1.2. Open File Mode
R, read-only mode (default). W, write-only mode. "Non-readable; does not exist; create; Delete content;" A, append mode. "Readable, non-existent, created;" "+" means that you can read and write to a file r+. "Readable, writable, optional" w+, read-----> No egg with a +, the same as a "U" means that when reading, \ r \ n \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ rur+u "B" means Linux negligible, required when Windows processes binaries) RB---> binary files read, binaries must be opened using binary mode Wbab different modes use exercise: Write a file with the following: I love Beijing Tian ' an gate Beijing Tian ' an door on the sun rising data = Open ("Yestday", encoding= "Utf-8"). Read ()---> Read all, default to R (read-only mode) print (data) execution result: I love Beijing Tian ' an gate Beijing Tian ' an door sun rising ***************** When the mode changes to write-only mode, it will prompt an error, unreadable, The contents of the Yestday file are emptied by data = open ("Yestday", ' W ', encoding= "Utf-8"). Read () print (data) execution result: IO. Unsupportedoperation:not readable****************************************************************************** data = open ("Yestday", ' a ', encoding= "Utf-8") data.write ("\nwelcome") execution Result: no return value, You can see if the Yestday file adds welcome characters to the tail ******************************************************************************** Yestday file contents: You are My baby! Come on!!! Come on!!! Come on!!! Ue.Comedata = open ("Yestday", ' r+ ', enconding= "Utf-8") print (Data.readline ()) print (Data.readline ()) Data.write ("\ Nbeijing ") Execution result: you are My baby! Come on!!! View Yestday file contents: You are My baby! Come on!!! Come on!!! Come on!!! welcomebeijing★★r+ read-write mode, can only be appended at the end, cannot be inserted inside the file, for example, read 2 lines above, and then write to Beijing, to see the changes in the file is at the end of the addition of the string.
1.3. Common functions of File objects

The file object is created using the Open function, which is a common function of the file object:

Serial Number Method Description
1 File.close () Close the file. The file can no longer read and write after closing.
2 File.flush () Refreshes the file internal buffer, directly writes the internal buffer data immediately to the file, rather than passively waits for the output buffer to write.
3 File.fileno () Returns an integer that is a file descriptor (Descriptor FD Integer) that can be used in some of the underlying operations, such as the Read method of the OS module.
4 File.isatty () Returns True if the file is connected to an end device, otherwise False is returned.
5 File.next () Returns the next line of the file
6 File.read ([size]) Reads the specified number of bytes from the file, if none is given or is negative.
7 File.readline ([size]) Reads the entire line, including the "\ n" character.
8 File.seek () Set the current location of the file
9 File.tell () Returns the current location of the file
10 File.truncate ([size]) Truncates from the beginning of the first line of the file, truncates the file to size characters, no size indicates truncation from the current position, and all characters after the truncation are deleted, where the line break under the Widnows system represents a 2-character size.
11 File.write (str) Writes a string to a file with no return value
12 File.writelines (Sequence) Writes a list of sequence strings to the file and adds a newline character to each line if a line break is required.

Progress bar Exercise:----> Flush buffer with flush

import sys,timefor i in range(10):    sys.stdout.write("#")    sys.stdout.flush()    time.sleep(0.1)执行结果:##########

File modification Exercises:----> by saving the modified content to a new file
The contents of the file are as follows:
I love Beijing Tiananmen
The sun rises on Tiananmen Square
Zhang jie
Jj
Jay

file = open("yestday","r",encoding="utf-8")filenew = open("yestdaynew",‘w‘,encoding="utf-8")for line in file:    if "张杰" in line:        line = line.replace("张杰","谢娜")    filenew.write(line)file.close()filenew.close()执行结果:生成了yestdaynew文件,内容如下:我爱北京天安门天安门上太阳升谢娜林俊杰周杰伦
1.4. With statement

To avoid forgetting to close a file after opening it, you can manage the context by:
with open(‘log‘,‘r‘) as f:
This way, when the with code block finishes executing, the internal automatically shuts down and frees the file resource.
After Python 2.7, with also supports the management of multiple file contexts simultaneously, namely:
with open(‘log1‘) as obj1, open(‘log2‘) as obj2:

with open("yestday",‘r‘,encoding="utf-8") as file:    for line in file:        print(line)执行结果:我爱北京天安门天安门上太阳升张杰林俊杰周杰伦with open("yestday",‘r‘,encoding="utf-8") as file ,        open("yestday",‘r‘,encoding="utf-8") as file2:    for line in file2:        print(line)执行结果:我爱北京天安门天安门上太阳升张杰林俊杰周杰伦
2. Character encoding and transcoding

Detailed article:
Python Encoding Ultimate Edition

2.1 Need to know

1. In Python2 the default encoding is ASCII, the default is Unicode in Python3

[[email protected] ~]# pythonPython 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> print(sys.getdefaultencoding())asciiC:\Users\Administrator>pythonPython 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import sys>>> print(sys.getdefaultencoding())utf-8>>>

2.unicode is divided into utf-32 (4 bytes), utf-16 (accounting for two bytes), Utf-8 (1-4 bytes), so utf-16 is now the most commonly used Unicode version, but in the file is still utf-8, because UTF8 save space

3. Encode in Py3, while transcoding will also change the string to bytes type, decode decoding will also turn bytes back to string

The relationship between 4.Unicode and UTF8:
Word: Unicode is a memory-encoded representation scheme (a specification), and UTF is a scheme for how to save and transmit Unicode (implementation), which is also the difference between UTF and Unicode.

in python 2.x#author = KIM#-*-coding:utf-8-*-  -->申明编码的方式为utf-8import sysprint(sys.getdefaultencoding())  -->python 2.x中默认的编码是asciimsg = "你好"msg_to_gbk = msg.decode("utf-8").encode("gbk")print(msg_to_gbk)执行结果:[[email protected] ~]# python encode.py ascii?o --->乱码此处将utf-8编码转为gbk的编码过程:msg解码(decode)为unicode,再编码(encode)为gbk。而默认的的编码格式为utf-8,gbk格式明文显示会出现乱码。in python 3.ximport sysprint(sys.getdefaultencoding()) msg = "我爱北京天安门"msg_gbk = msg.encode("gbk")  -->默认就是unicode,不需要decodemsg_utf8 = msg.encode("utf-8")msg_unicode = msg.encode("gbk").decode("gbk")print(msg_gbk)print(msg_utf8 )print(msg_unicode)执行结果:utf-8b‘\xce\xd2\xb0\xae\xb1\xb1\xbe\xa9\xcc\xec\xb0\xb2\xc3\xc5‘b‘\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8‘我爱北京天安门

Python Introductory article (v) file manipulation and character encoding

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.