Python100Days study notes --- Day11 file and exceptions

The actual development often encounter scene data persistence operations, and data persistence most straightforward way is to save the data to a file. When it comes to the "File" is the word, you may need to look at science knowledge about the file system, but here we do not waste ink introduced this concept, please find out about yourself through Wikipedia.

Implementation file read and write operations in Python is actually very simple, via Python built-in open function, we can specify a file name, operating mode, and other encoded information to obtain an object file operations, then you can read and write to the file . The mode of operation here is simply more of what file (character or binary data) and what kind of operation do (read, write or append), specifically in the following table open.

操作模式	具体含义
'r'	    读取 (默认)
'w'	    写入(会先截断之前的内容)
'x'	    写入,如果文件已经存在会产生异常
'a'	    追加,将内容写入到已有文件的末尾
'b'  	二进制模式
't'	    文本模式(默认)
'+'	    更新(既可以读又可以写)

The picture below from the newbie tutorial website, it shows if the application needs to set the operating mode.

Here Insert Picture Description
Read and write text file
when reading a text file, needs to be specified when the open function with good path to the file name (use a relative or absolute path) and the file mode to 'R & lt' (if not specified, the default value is' r '), then specify the encoding parameters by encoding (if not specified, the default value is None, then used in the reading file of the operating system default encoding), encoding used to save the file can not be guaranteed if the encoding parameter specifies the coding is consistent, then it may lead to inability to decode the character and read failure. The following example demonstrates how to read a plain text file.

def main():
    f = open('致橡树.txt', 'r', encoding='utf-8')
    print(f.read())
    f.close()


if __name__ == '__main__':
    main()

Please note that the above code, if the open function specified file does not exist or can not be opened, then an abnormal condition causes the program to crash. In order to make the code a certain degree of robustness and fault tolerance, we can use the exception mechanism of Python code situation may occur at runtime appropriate treatment, as shown below.

def main():
    f = None
    try:
        f = open('致橡树.txt', 'r', encoding='utf-8')
        print(f.read())
    except FileNotFoundError:
        print('无法打开指定的文件!')
    except LookupError:
        print('指定了未知的编码!')
    except UnicodeDecodeError:
        print('读取文件时解码错误!')
    finally:
        if f:
            f.close()


if __name__ == '__main__':
    main()

In Python, we can code that situation may occur at run time in a try code block, after the try code block can keep up with one or more except to capture abnormal situation that may arise. For example, in the above process of reading the file, the file can not find the cause FileNotFoundError, An unknown encoding will lead LookupError, if not caused UnicodeDecodeError as specified decoding will be reading the file, we try to keep up with three at the back except one were treated three different anomalies. Finally, we use the finally block to close open files, freed of external resources program acquisition, due to the code in the finally block regardless of the normal procedure or an exception will be executed exit function to (or even call the sys module exit Python environment, the finally block will be performed because the exit function is essentially caused SystemExit an exception), so we finally blocks usually referred to as "always execute the code block", it is the most suitable for applications operating releasing external resources. If you do not want to close the file objects in a finally block the release of resources, may also use the context grammar, with the keyword specified by the file object context and automatically release resources when leaving the file context, the code shown below.

def main():
    try:
        with open('致橡树.txt', 'r', encoding='utf-8') as f:
            print(f.read())
    except FileNotFoundError:
        print('无法打开指定的文件!')
    except LookupError:
        print('指定了未知的编码!')
    except UnicodeDecodeError:
        print('读取文件时解码错误!')


if __name__ == '__main__':
    main()

In addition to the read method reads file using the file object may also be used for-in loop or a file read row by row reading method readlines row to a container list, the code shown below

import time


def main():
    # 一次性读取整个文件内容
    with open('致橡树.txt', 'r', encoding='utf-8') as f:
        print(f.read())

    # 通过for-in循环逐行读取
    with open('致橡树.txt', mode='r') as f:
        for line in f:
            print(line, end='')
            time.sleep(0.5)
    print()

    # 读取文件按行读取到列表中
    with open('致橡树.txt') as f:
        lines = f.readlines()
    print(lines)
    

if __name__ == '__main__':
    main()

To write a text message File is very simple, good specify the file name and file mode to 'w' to open when using the function. Note that if necessary for writing an additional file content type, the mode should be set to 'a'. If the file to be written does not exist it will automatically create a file instead of raising it. The following example demonstrates how the prime numbers between 1-9999 were written to three files (prime number between 1-99 stored in a.txt, the prime numbers between 100 to 999 stored in the b.txt, between 1000 and 9999 primes stored in the c.txt).

from math import sqrt


def is_prime(n):
    """判断素数的函数"""
    assert n > 0
    for factor in range(2, int(sqrt(n)) + 1):
        if n % factor == 0:
            return False
    return True if n != 1 else False


def main():
    filenames = ('a.txt', 'b.txt', 'c.txt')
    fs_list = []
    try:
        for filename in filenames:
            fs_list.append(open(filename, 'w', encoding='utf-8'))
        for number in range(1, 10000):
            if is_prime(number):
                if number < 100:
                    fs_list[0].write(str(number) + '\n')
                elif number < 1000:
                    fs_list[1].write(str(number) + '\n')
                else:
                    fs_list[2].write(str(number) + '\n')
    except IOError as ex:
        print(ex)
        print('写文件时发生错误!')
    finally:
        for fs in fs_list:
            fs.close()
    print('操作完成!')


if __name__ == '__main__':
    main()

Reading and Writing Binary
know how to read and write text files to read and write binary files it is very simple, the following code implements a function to copy the image file.

def main():
    try:
        with open('guido.jpg', 'rb') as fs1:
            data = fs1.read()
            print(type(data))  # <class 'bytes'>
        with open('吉多.jpg', 'wb') as fs2:
            fs2.write(data)
    except FileNotFoundError as e:
        print('指定的文件无法打开.')
    except IOError as e:
        print('读写文件时出现错误.')
    print('程序执行结束.')


if __name__ == '__main__':
    main()

JSON file to read and write
by the above explanation, we already know how to save text data and binary data to a file, then there's a problem, if you want to save a list or a data dictionary to a file, how should do it? The answer is to save data in JSON format. JSON is "JavaScript Object Notation" acronym, it was originally created JavaScript object literal syntax of one language, has now been exchanged cross-language, cross-platform data widely used, the reason is very simple, because JSON is plain text, any system in any programming language plain text is not a problem. Currently JSON basically has replaced XML as the de facto standard for exchanging data between heterogeneous systems. Knowledge of JSON, JSON more can refer to the official website, this website can also understand that each language processing tools-party libraries or JSON data format that can be used, the following is a simple example of JSON.

{
    "name": "骆昊",
    "age": 38,
    "qq": 957658,
    "friends": ["王大锤", "白元芳"],
    "cars": [
        {"brand": "BYD", "max_speed": 180},
        {"brand": "Audi", "max_speed": 280},
        {"brand": "Benz", "max_speed": 320}
    ]
}

We may have noticed, the above JSON with Python in dictionary is actually the same as the same, in fact JSON data types and data types Python is very easy to find the corresponding relationship, as shown in the following two tables.

JSON					Python
object					dict
array					list
string					str
number (int / real)		int / float
true / false			True / False
null					None
Python									JSON
dict									object
list, tuple								array
str										string
int, float, int- & float-derived Enums	number
True / False							true / false
None									null

We use the Python json module can be saved in the dictionary or list JSON format to a file, code is as follows.

import json


def main():
    mydict = {
        'name': '骆昊',
        'age': 38,
        'qq': 957658,
        'friends': ['王大锤', '白元芳'],
        'cars': [
            {'brand': 'BYD', 'max_speed': 180},
            {'brand': 'Audi', 'max_speed': 280},
            {'brand': 'Benz', 'max_speed': 320}
        ]
    }
    try:
        with open('data.json', 'w', encoding='utf-8') as fs:
            json.dump(mydict, fs)
    except IOError as e:
        print(e)
    print('保存数据完成!')


if __name__ == '__main__':
    main()

json module There are four important functions, namely:

dump - 将Python对象按照JSON格式序列化到文件中
dumps - 将Python对象处理成JSON格式的字符串
load - 将文件中的JSON数据反序列化成对象
loads - 将字符串的内容反序列化成Python对象

Here there were two concepts, one called serialization, called deserialization. Wikipedia, the free encyclopedia Wikipedia on the two concepts is explained: "Serialization (serialization) in computer science data processing means to convert the data structure or object state to be stored or transmitted in the form, so that when they need to return to the original state, and by a sequence of data bytes reacquisition, these bytes can be used to generate a copy of the original object (copy). in contrast to this process operation, i.e., from a series of bytes operation extracting data structures, is deserialized (deserialization). "

Currently the vast majority of network data services (or so-called network API) are providing data in JSON format based on the HTTP protocol, HTTP protocol on the knowledge, you can see the "Getting Started HTTP protocol" Ruan Yifeng teacher, if you want to understand the domestic network data services, you can take a look at aggregate data and avatar data and other sites abroad can see {API} Search website. The following example demonstrates how to use requests module (package was good enough third-party network access module) to access the network API to get national news, how to parse JSON data via json module and display news headlines, this example uses data provided by the domestic day trip News data interface, which APIKey needs its own application to the site.

import requests
import json


def main():
    resp = requests.get('http://api.tianapi.com/guonei/?key=APIKey&num=10')
    data_model = json.loads(resp.text)
    for news in data_model['newslist']:
        print(news['title'])


if __name__ == '__main__':
    main()

Implemented in Python to serialization and deserialization json module except, you can also use the pickle and shelve modules, but the use of these two modules are specific to a sequence of protocol data sequence, so only the serialized data Python can be identified. Knowledge about these two modules can look at the data on the network itself. In addition, if you want to learn more knowledge about Python exception mechanism, you can see segmentfault above article "concluded: Python exception handling," the article not only describes the use exception mechanism of Python, also summed up a series of best practices, it is worth reading.

Published 124 original articles · won praise 141 · views 160 000 +

Guess you like

Origin blog.csdn.net/weixin_36838630/article/details/105206725