码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Python读写文本、图片、xml


    Python操作文本、图片、xml

    • 1.Python读写文本
      • 1.1文本读取
      • 1.2文本写入
    • 2.Python读取、显示图片
    • 3.Python读写Xml

    (1)Python读写文本
    (2)Python读取、显示图片
    (3)Python读写Xml

    1.Python读写文本

    新建helloworld.txt,创建file.py文件用于读写helloworld.txt。
    请添加图片描述

    1.1文本读取

    1.在helloworld.txt中写入内容“你好呀!!!,wxl”。在file.py中编写程序读取helloworld.txt中内容。

    f = open("helloworld.txt",mode="r")
    data = f.read()
    print(data)
    

    2.运行结果:

    Traceback (most recent call last):
      File "/home/tarena/PycharmProjects/爬虫/file.py", line 1, in <module>
        f = open("helloworld.txt",mode="r")
    FileNotFoundError: [Errno 2] No such file or directory: 'helloworld.txt'
    

    3.在右上交选择file->Edit Configurations,修改工作路径(working directory)到当前文件夹:
    在这里插入图片描述
    4.运行结果:

    你好呀!!!,wxl
    
    Process finished with exit code 0
    

    1.2文本写入

    f = open("helloworld.txt",mode="w")
    f.write("我们朋友!!!")
    f.close()
    
    f = open("helloworld.txt",mode="r")
    data = f.read()
    print(data)
    

    运行结果:

    我们朋友!!!
    
    Process finished with exit code 0
    

    2.Python读取、显示图片

    使用PIL家族读取图片。

    from PIL import Image
    
    img = Image.open("./5.jpg")
    img.show()
    

    显示效果:
    请添加图片描述

    3.Python读写Xml

    from lxml import etree
    
    #写入XML文件
    # 创建根元素和子元素
    root = etree.Element('root')
    child1 = etree.SubElement(root, '学生1')
    child1.set('年龄', '20')
    child1.text = '小明'
    child2 = etree.SubElement(root, '学生2')
    child2.set('年龄', '25')
    child2.text = '小王'
    
    # 将元素写入文件
    tree = etree.ElementTree(root)
    tree.write('output.xml', encoding='utf-8', xml_declaration=True)
    
    # 解析XML文件
    tree = etree.parse('output.xml')
    root = tree.getroot()  # 获取根元素
    for child in root:  # 遍历子元素
        print(child.tag, child.attrib)
    

    运行结果:

    output.xml文件内容
    <?xml version='1.0' encoding='UTF-8'?>
    <root><学生1 年龄="20">小明</学生1><学生2 年龄="25">小王</学生2></root>
    
    学生1 {'年龄': '20'}
    学生2 {'年龄': '25'}
    
    Process finished with exit code 0
    
  • 相关阅读:
    小程序中的自定义组件以及组件通信、数据共享、插槽、behaviors
    Springboot接入ChatGPT 续
    08-React扩展
    链表【Linked List】
    【报错】django.db.migrations.exceptions.NodeNotFoundError:
    按钮测试: 循环遍历 单据行明细 &执行SQL
    基于Flask+Echarts的中药材价格分析与可视化项目
    js-判断android,ios,pc三端环境
    Docker部署jar包
    PCL点云滤波处理D435i深度图用于octomap
  • 原文地址:https://blog.csdn.net/weixin_44875787/article/details/139969218
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号