当前位置: 首页 > news >正文

python读写各种格式文件

文章目录

  • 前言
  • 1 读写txt
  • 2 读写xml文件
  • 3 读写json文件
  • 4 命名/拷贝移动文件
  • 总结

前言

 本篇博文主要记录一些常见的文件读取操作,不定期更新扩充。


1 读写txt

import os
# 读
def read_txt(path):
    with open(path,'r') as f:
        while True:
            line = f.readline()      # 读取整行数据
            if not line:
                break
            imgsets.append(line.strip()) # 必须放在 if 之后,否则会多读入一个空字符。
        # 或者
        lines = f.readlines()
        for line in lines:
        	imgsets.append(line.strip())
    #f.close() # f.close不用加,因为Python自动默认将操作系统的待写入的文件写到了磁盘里面。
# 写
def write_txt(path):
    with open(path,'w') as f:
        f.write('wulele\n')
    #f.close() # 和上面同理,with方法保证了 操作系统自动将文件完全写入磁盘了。
if __name__ == '__main__':
    # read
    read_path = '/home/wujian/MMLAB/gallery_content.txt'
    read_txt(read_path)
    # write
    write_path = '/home/wujian/MMLAB/save.txt'
    write_txt(write_path)

2 读写xml文件

import xml.etree.ElementTree as ET
def save_to_new_annotations(img,path):
    doc = ET.parse(path)
    root = doc.getroot()
    width,height,_ = img.shape

    for size in root.findall('size'):
        size.find('width').text = str(width)
        size.find('height').text = str(height)

    f = 'E:/RotateVocDataset/VOCdevkit/VOC2007/NewAnnotations/000005.xml'
    doc.write(f,encoding="utf-8")                         #重新写入参数文件
    print('Done!')

def read_annotation(path):
    root = ET.parse(path).getroot()
    sizes = []
    filename = root.find('filename').text   # text后无括号
    # 获取图像的尺寸
    for size in root.findall('size'):
        width = size.find('width').text
        height = size.find('height').text
        channel = size.find('depth').text
    sizes.extend([width,height,channel])
    # 获取物体信息
    imdbs = []
    for obj in root.findall('object'):
        imdb = []
        label = obj.find('name').text
        bbox = obj.find('bndbox')          # 由于bndbox非叶子节点,所以不能直接.text
        xmin = bbox.find('xmin').text      # 获取bbox的信息
        ymin = bbox.find('ymin').text
        xmax = bbox.find('xmax').text
        ymax = bbox.find('ymax').text
        imdb.extend([xmin,ymin,xmax,ymax,label])
        imdbs.append(imdb)
    return sizes,imdbs

if __name__ == '__main__':
    img_path= 'E://RotateVocDataset/VOCdevkit/VOC2007/JPEGImages/000001.jpg'
    anno_path = 'E://RotateVocDataset/VOCdevkit/VOC2007/Annotations/000001.xml'
    img=cv2.imread(img_path)
    height, width ,_ = img.shape
    size, imdbs = read_annotation(anno_path)
    print(size,'\n',imdbs)

3 读写json文件

json最大好处就是能够存储python的数据结构,即写进去是dict,读出来依旧是dict,所以大多数数据集都喜欢以json进行保存。而其中用到的主要api是:json.dumps(), json.loads()。

root_dir = '/mnt/data/VOC2017/train/'
sjs  = root_dir + 'coco_hoi_train2017.json'
# 写入json
with open(sjs, 'w') as f:
    #f.write(str(annot) + '\n')             # 写的方法会破坏python的数据结构
    f.write(json.dumps(content) + '\n')     # json.dumps 就能保存python的数据结构
# 读取json
with open(json_path,'r') as f:
    lines = f.readlines()
    for line in lines:
        content = json.loads(line.strip())
        print(content['img'], content['label'])
        break

4 命名/拷贝移动文件

filename.zfill(6)  # 字符串填充6位,右对齐。

shutile.copyfile

总结

 没啥可总结的…

相关文章:

  • 中通云的容器化之旅:从单集群到多集群的演进
  • 第2章 深入理解Thread构造函数
  • 苍穹外卖知识点
  • 【Linux】HTTP:Cookie 和 Session 详解
  • tmux和vim的基本操作
  • 【Dubbo+Zookeeper】——SpringBoot+Dubbo+Zookeeper知识整合
  • 【蓝桥杯集训·每日一题2025】 AcWing 6134. 哞叫时间II python
  • 算法-哈希表篇08-四数之和
  • GEE | 基于ASTER影像提取矿物指数
  • 【算法】788. 逆序对的数量
  • DeepSeek 的架构思维与java架构的思考
  • Redis7——基础篇(六)
  • Redis常见数据结构及其在项目中用法
  • 软件测试工程师职业规划:从入门到卓越
  • 【Deepseek】Linux 本地部署 Deepseek
  • 第37天:安全开发-JavaEE应用JNDI注入RMI服务LDAP服务JDK绕过调用链类
  • 【图像处理】:两幅图中相同区域的相似度比较
  • 矩阵加减乘除的意义与应用
  • 初尝git自结命令大全与需要理解的地方记录
  • 【爬虫 JS 逆向实战 - 请求载荷加密详细教程】
  • 一周人物|卡鲁等入围英国特纳奖,李学明新展中国美术馆
  • 国务院任免国家工作人员:饶权任国家文物局局长
  • 北美票房|《罪人》遭媒体唱衰,好莱坞业内人士集体反击
  • 绵阳造AI机器狗参与警务工作,演练中辅助民警控制“嫌疑人员”
  • 一季度全国城镇新增就业308万人
  • 中国海警局新闻发言人就菲律宾非法登临铁线礁发表谈话