zoukankan      html  css  js  c++  java
  • python文件读read()、readline()、readlines()对比

      读取文件的三个方法:read()、readline()、readlines()。均可接受一个变量用以限制每次读取的数据量,但通常不使用。本章目的是分析和总结三种读取方式的使用方法和特点。

    一、read方法

      特点是:读取整个文件,将文件内容放到一个字符串变量中。

      劣势是:如果文件非常大,尤其是大于内存时,无法使用read()方法。

    file = open('兼职模特联系方式.txt', 'r')  # 创建的这个文件,也是一个可迭代对象
    
    try:
        text = file.read()  # 结果为str类型
        print(type(text))
        print(text)
    finally:
        file.close()
    """
    <class 'str'>
    吴迪 177 70 13888888
    王思 170 50 13988888
    白雪 167 48 13324434
    黄蓉 166 46 13828382
    """

      read()直接读取字节到字符串中,包括了换行符

    >>> file = open('兼职模特联系方式.txt', 'r')
    >>> a = file.read()
    >>> a
    '吴迪 177 70 13888888
    王思 170 50 13988888
    白雪 167 48 13324434
    黄蓉 166 46 13828382'

    二、readline方法

      特点:readline()方法每次读取一行;返回的是一个字符串对象,保持当前行的内存

      缺点:比readlines慢得多

    file = open('兼职模特联系方式.txt', 'r')
    
    try:
        while True:
            text_line = file.readline()
            if text_line:
                print(type(text_line), text_line)
            else:
                break
    finally:
        file.close()
    """
    <class 'str'> 吴迪 177 70 13888888
    
    <class 'str'> 王思 170 50 13988888
    
    <class 'str'> 白雪 167 48 13324434
    
    <class 'str'> 黄蓉 166 46 13828382
    """

      readline()  读取整行,包括行结束符,并作为字符串返回

    >>> file = open('兼职模特联系方式.txt', 'r')
    >>> a = file.readline()
    >>> a
    '吴迪 177 70 13888888
    '

    三、readlines方法

      特点:一次性读取整个文件;自动将文件内容分析成一个行的列表

    file = open('兼职模特联系方式.txt', 'r')
    
    try:
        text_lines = file.readlines()
        print(type(text_lines), text_lines)
        for line in text_lines:
            print(type(line), line)
    finally:
        file.close()
    """
    <class 'list'> ['吴迪 177 70 13888888
    ', '王思 170 50 13988888
    ', '白雪 167 48 13324434
    ', '黄蓉 166 46 13828382']
    <class 'str'> 吴迪 177 70 13888888
    
    <class 'str'> 王思 170 50 13988888
    
    <class 'str'> 白雪 167 48 13324434
    
    <class 'str'> 黄蓉 166 46 13828382
    """

      readlines()读取所有行然后把它们作为一个字符串列表返回。

    >>> file = open('兼职模特联系方式.txt', 'r')
    >>> a = file.readlines()
    >>> a
    ['吴迪 177 70 13888888
    ', '王思 170 50 13988888
    ', '白雪 167 48 13324434
    ', '黄蓉 166 46 13828382']
  • 相关阅读:
    二十八,GeoWebCache blob文件仓库(/blobstores)
    二十七,Geoserver 用户和组(/roles)
    二十六,Geoserver 用户和组(/usergroup)
    二十五,Geoserver 工作空间(/workspaces)
    二十四,Geoserver wmts仓库(/wmtsstores)
    二十三,Geoserver wmts仓库图层(/wmtslayers)
    二十二,Geoserver wms仓库(/wmsstores)
    二十一,Geoserver wms仓库图层(/wmslayers)
    [转]delphi 防止刷新时闪烁的终极解决办法
    VC GDI 像素转厘米(英寸)[转发]
  • 原文地址:https://www.cnblogs.com/xiugeng/p/8635862.html
Copyright © 2011-2022 走看看