Python diary----2017.7.24

Source: Internet
Author: User
Tags readable

Character encoding

1 What encoding is stored and what code will be taken out
PS: Memory is fixed using Unicode encoding,
We can control the encoding is to the hard disk storage or based on the network transmission selection code

2 data is first generated in memory and is in Unicode format, to be transferred to bytes format
#unicode----->encode (utf-8)------>bytes
If you get bytes, you can store it in a file or network-based transmission.
#bytes------>decode (GBK)------->unicode

3 Python3 string is recognized as Unicode
The string encode in Python3 gets bytes

4 See
The string in Python2 is bytes
Python2 in front of the string is Unicode

File processing

1. Read-only mode ' R ': can only read the file, the file does not exist will be an error.

Read (): Read file

Open (): Opening file

Close (): Close file {Be sure to close}

ReadLine (): Execution of a row

ReadLines (): reads all the lines, and saves them in the form of a list

Readable: () detects whether the file is readable, returns TRUE or False

2. write-only mode; ' W ', file does not exist then create, empty existing

Writeable (): detects if the file is writable, returns TRUE or False

The Write (str) argument is a string that is the content you want to write to the file.

The Writelines (sequence) parameter is a sequence, such as a list, that iterates over the file for you.

3. Append mode ' A ': The file does not exist then add if the file exists and the file content is appended at the end.

RB: Read
F=open (' aaaa.py ', ' RB ')
Print (F.read (). Decode (' Utf-8 '))

F=open (' 1.jpg ', ' RB ')
Data=f.read ()

WB: Write
F=open (' 2.jpg ', ' WB ')
F.write (data)
F=open (' New_3.txt ', ' WB ')
F.write (' aaaaa\n '. Encode (' Utf-8 '))

WB: Append Write
F=open (' new_3.txt ', ' AB ')
F.write (' aaaaa\n '. Encode (' Utf-8 '))

Context Management:

  with open (' aaaa.py ', ' R ', encoding= ' Utf-8 ') as read_f,\ 
Open (' aaaa_new.py ', ' W ', encoding= ' utf-8 ') as writ E_f:
Data=read_f.read ()
Write_f.write (data)


loops through each line of content
with open (' A.txt ', ' R ', encoding= ' Utf-8 ') as F:
while True:
Line=f.readline ()
If not line:break
print (line,end= ")
Lines=f.readlines () #只适用于小文件
Print (lines)

Data=f.read ()
Print (type data)


For line in F: #推荐使用
Print (line,end= ')
Modification of files
Mode One: Applies only to small files
Import OS
With open (' A.txt ', ' R ', encoding= ' Utf-8 ') as read_f,\
Open (' A.txt.swap ', ' W ', encoding= ' Utf-8 ') as Write_f:
Data=read_f.read ()
Write_f.write (Data.replace (' alex_sb ', ' ALEX_BSB '))

Os.remove (' A.txt ')
Os.rename (' A.txt.swap ', ' a.txt ')


Way two:
Import OS
With open (' A.txt ', ' R ', encoding= ' Utf-8 ') as read_f,\
Open (' A.txt.swap ', ' W ', encoding= ' Utf-8 ') as Write_f:
For line in Read_f:
Write_f.write (Line.replace (' ALEX_BSB ', ' BB_ALEX_SB '))

Os.remove (' A.txt ')
Os.rename (' A.txt.swap ', ' a.txt ')


Python diary----2017.7.24

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.