python:读取txt文本中的每一行的同时,对每一行以空格进行分割

文本中格式:一行中有许多列

ADE_train_00000001 airport_terminal
ADE_train_00000002 airport_terminal
ADE_train_00000003 art_gallery
ADE_train_00000004 badlands
ADE_train_00000005 ball_pit
ADE_train_00000006 bathroom
ADE_train_00000007 bathroom
ADE_train_00000008 bathroom
ADE_train_00000009 bathroom
ADE_train_00000010 bathroom
ADE_train_00000011 bathroom
ADE_train_00000012 bathroom
ADE_train_00000013 bathroom
ADE_train_00000014 bathroom

读取txt文本中的每一行的同时,对每一行进行切割为多列

images = []
if os.path.isfile(file_path):
    with open(file_path,'r') as f:
        for line in f.readlines():
            data = line.split('/t/n')
            for str in data:
                sub_str = str.split(' ')
            if sub_str:
                images.append(sub_str)

小工程:依据sceneCategories.txt文件,将路径中为outdoor标志的图片路径重建到另一个文件夹中。
在这里插入图片描述

import os
import shutil

###################################################
root = '/home/ada/kah/Datasets/ADEChallengeData2016'
file_path = os.path.join(root, 'sceneCategories.txt')
dir_root = os.path.join(root,'ADE20K_outdoor')

images = []
if os.path.isfile(file_path):
    with open(file_path,'r') as f:
        for line in f.readlines():
            data = line.split('/t/n')
            for str in data:
                sub_str = str.split(' ')
            if sub_str:
                images.append(sub_str)

# flag = 'images'
flag = 'annotations'
for image in images:
    if '_outdoor' in image[1]:
        label = 'training' if 'train' in image[0] else 'validation'
        dir_path = os.path.join(dir_root,flag,label)
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
        exp = '.jpg' if 'images' in flag else '.png'
        src_path = os.path.join(root,flag,label,image[0]+exp)
        dst_path = os.path.join(dir_path,image[0]+exp)
        shutil.copy(src_path,dst_path)
print()
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码小白的成长

计算机网络PPT下载

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值