码农知识堂 - 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
    
  • 相关阅读:
    【面向校招】Golang 常考面试题汇总 持续更新中...
    SYSCALL SWAPGS
    【电源专题】开关模式电源电流检测基础及感应电阻放置位置选择
    【产品】家用工商业储能计量表ADL3000-E-B系列导轨式多功能电能表 UL认证 /CE认证 /485通讯
    滤波算法_扩展卡尔曼滤波(EKF, Extended Kalman filter)_全网最详细的数学推导_Part1
    数据库用户管理
    jupyter notebook代码自动换行,超过一行长度自动换行,不用左右滑动
    【C++】list基本接口+手撕 list(详解迭代器)
    【收藏】50多个免费API接口分享
    多维数组和交错数组笔记
  • 原文地址:https://blog.csdn.net/weixin_44875787/article/details/139969218
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号