python读取txt文件readlines()和readline()方法练习

记录一下自己的笔记:

如果刚接触readlines()和readline()可以参考我的前面一篇博客:https://blog.csdn.net/li_haiyu/article/details/80366067

一、假装我是标题1

随便创建一个txt文件练习就可以了,为了方便我用我电脑存在的vol_classes.txt文件


思路:

这里行数比较多,最后的目的是将他们全部转换成为列表的形式首先想到的就是采用readlines()方法,因为这个方法是将所有的内容读取并返回到列表中,读取的每一行元素作为列表中的每一个元素。

code:

classes_path = "..//model_data//coco_classes.txt"
with open(classes_path) as f:
    class_names = f.readlines()# 全部读取最后返回一个列表存所有的类,每行后面都会带有“\n”
    print(type(class_names))
    print(class_names)

    f.close()

class_names = [c.strip() for c in class_names]# 采用strip()方法可以去掉最后的“\n”
print(type(class_names))
print(class_names)

output:

<class 'list'>
['person\n', 'bicycle\n', 'car\n', 'motorbike\n', 'aeroplane\n', 'bus\n', 'train\n', 'truck\n', 'boat\n', 'traffic light\n', 'fire hydrant\n', 'stop sign\n', 'parking meter\n', 'bench\n', 'bird\n', 'cat\n', 'dog\n', 'horse\n', 'sheep\n', 'cow\n', 'elephant\n', 'bear\n', 'zebra\n', 'giraffe\n', 'backpack\n', 'umbrella\n', 'handbag\n', 'tie\n', 'suitcase\n', 'frisbee\n', 'skis\n', 'snowboard\n', 'sports ball\n', 'kite\n', 'baseball bat\n', 'baseball glove\n', 'skateboard\n', 'surfboard\n', 'tennis racket\n', 'bottle\n', 'wine glass\n', 'cup\n', 'fork\n', 'knife\n', 'spoon\n', 'bowl\n', 'banana\n', 'apple\n', 'sandwich\n', 'orange\n', 'broccoli\n', 'carrot\n', 'hot dog\n', 'pizza\n', 'donut\n', 'cake\n', 'chair\n', 'sofa\n', 'pottedplant\n', 'bed\n', 'diningtable\n', 'toilet\n', 'tvmonitor\n', 'laptop\n', 'mouse\n', 'remote\n', 'keyboard\n', 'cell phone\n', 'microwave\n', 'oven\n', 'toaster\n', 'sink\n', 'refrigerator\n', 'book\n', 'clock\n', 'vase\n', 'scissors\n', 'teddy bear\n', 'hair drier\n', 'toothbrush\n']
<class 'list'>
['person', 'bicycle', 'car', 'motorbike', 'aeroplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'sofa', 'pottedplant', 'bed', 'diningtable', 'toilet', 'tvmonitor', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']

二、我是标题2

结果发现还有个yolo_anchor.txt文件,上面使用了readlines()方法,下面通过这个txt文件比较一下readlines()和readline()方法

思路:

目的是读取里面的数据,然后转化为二维数组如:[10. ,13.],[16. ,30.],这里只有一行,所以想到的是采用readline()方法,返回的是string 只读取即可,当然也可以采用readlines()方法,但是注意返回是列表哦,下面可以比较一下

① readline()方法code:

import numpy as np
with open("..//model_data//yolo_anchors.txt") as f:
    anchors = f.readline()
    print(type(anchors))# string
    print(anchors)

    # 使用split()方法可以将字符串按照某种格式分开并返回列表
    anchors = anchors.split(',')
    print(type(anchors))
    print(anchors)

    # 转换为字符数据类型,然后再转换为列表格式,当然也可以使用list()代替[].
    anchors = [float(x) for x in anchors]
    print(type(anchors))# list
    print(anchors)

    # 转换成n行两列
    anchors = np.array(anchors).reshape(-1, 2)
    print(type(anchors))# 数组
    print(anchors)

output:

<class 'str'>
10,13,  16,30,  33,23,  30,61,  62,45,  59,119,  116,90,  156,198,  373,326

<class 'list'>
['10', '13', '  16', '30', '  33', '23', '  30', '61', '  62', '45', '  59', '119', '  116', '90', '  156', '198', '  373', '326\n']
<class 'list'>
[10.0, 13.0, 16.0, 30.0, 33.0, 23.0, 30.0, 61.0, 62.0, 45.0, 59.0, 119.0, 116.0, 90.0, 156.0, 198.0, 373.0, 326.0]
<class 'numpy.ndarray'>
[[ 10.  13.]
 [ 16.  30.]
 [ 33.  23.]
 [ 30.  61.]
 [ 62.  45.]
 [ 59. 119.]
 [116.  90.]
 [156. 198.]
 [373. 326.]]

② readlines()方法code:

import numpy as np
with open("..//model_data//yolo_anchors.txt") as f:
    anchors = f.readlines()
    print(type(anchors))
    print(anchors)# 输出一个列表,只有一个元素

    anchors = str(anchors[0])# 获取第一个元素
    print(anchors)

    anchors = anchors.split(',')
    print(anchors)

    anchors = [float(x) for x in anchors]
    print(anchors)
    
    print(np.array(anchors).reshape(-1,2))

output:

<class 'list'>
['10,13,  16,30,  33,23,  30,61,  62,45,  59,119,  116,90,  156,198,  373,326\n']
<class 'str'>
10,13,  16,30,  33,23,  30,61,  62,45,  59,119,  116,90,  156,198,  373,326

<class 'list'>
['10', '13', '  16', '30', '  33', '23', '  30', '61', '  62', '45', '  59', '119', '  116', '90', '  156', '198', '  373', '326\n']
[10.0, 13.0, 16.0, 30.0, 33.0, 23.0, 30.0, 61.0, 62.0, 45.0, 59.0, 119.0, 116.0, 90.0, 156.0, 198.0, 373.0, 326.0]
[[ 10.  13.]
 [ 16.  30.]
 [ 33.  23.]
 [ 30.  61.]
 [ 62.  45.]
 [ 59. 119.]
 [116.  90.]
 [156. 198.]
 [373. 326.]]
The end.




猜你喜欢

转载自blog.csdn.net/li_haiyu/article/details/80389498