current position:Home>Python series tutorials 122

Python series tutorials 122

2022-01-30 20:40:27 AI technology of artificial intelligence

friends , If you need to reprint, please indicate the source :blog.csdn.net/jiangjunsho…

Statement : During the teaching of artificial intelligence technology , Many students mentioned to me python Related issues , So in order to let the students master more extended knowledge and better understand AI technology , I put the assistant in charge of sharing this python Series of tutorials , I hope that helps ! Because of this python The tutorial wasn't written by me , So it's not as good as mine AI Technology teaching is humorous , It's boring to learn ; But its knowledge is in place , It's also worth reading !PS: If you don't understand this article, please read the previous article first , Step by step, learn a little everyday, and you won't find it difficult !

stay Python 3.0 and Python 2.6 in , File types are defined by open The second parameter of determines , This parameter can be called a pattern string , If the string contains a “b” Then it means opening the file in binary form , Otherwise, it is in the form of text .

Python Text and binary files have always been supported , But in Python 3.0 in , There is a clear difference between the two : • Text files represent content as regular str character string , Automatic execution Unicode Encoding and decoding , And the last line conversion is performed by default . • Binary files represent the content as a special bytes String type , And allow the program to access the contents of the file without modification .

If you need to handle internationalized applications or byte oriented data ,Python 3.0 Differences in affect the code . Usually , You have to use bytes String processing binaries , And use the regular str String processing text file . Besides , Because the text file realizes Unicode code , So you can't open a binary data file in text mode —— Otherwise, decode its contents into Unicode Text may fail .

Let's look at an example . When you read a binary data file , Will get a bytes object :

>>> data = open('data.bin','rb').read()       # Open binary file: rb=read binary

>>> data              # bytes string holds binary data

b'\x00\x00\x00\x07spam\x00\x08'

>>> data[4:8]             # Act like strings

b'spam'

>>> data[0]      # But really are small 8-bit integers

115

>>> bin(data[0])         # Python 3.0 bin() function

'0b1110011'
 Copy code 

Besides , Binary files do not perform any last line conversion on data . Therefore, when reading and writing files, it is necessary to distinguish whether to use text form or binary form . A file written as text , Also use text when reading . A file written in binary form , When reading, it is necessary to use binary form !

copyright notice
author[AI technology of artificial intelligence],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201302040260096.html

Random recommended