Python文件操作相关

  • 文件操作
  • 文件夹和路径
  • csv格式文件
  • ini格式文件
  • xml格式文件
  • excel文件

1. 文件操作

在学习文件操作之前,先来回顾一下编码的相关以及先关数据类型的知识。

  • 字符串类型(str),在程序中用于表示文字信息,本质上是unicode编码中的二进制。

    name = "刘小伟"
    
  • 字节类型(bytes)

    • 可表示文字信息,本质上是utf-8/gbk等编码的二进制(对unicode进行压缩,方便文件存储和网络传输。)

      name = "刘小伟"
      data = name.encode('utf-8')
      print(data) # b'\xe5\x88\x98\xe5\xb0\x8f\xe4\xbc\x9f'result = data.decode('utf-8')
      print(result) # "刘小伟"
      
    • 可表示原始二进制(图片、文件等信息)

1.1 读文件

  • 读文本文件

    # 1.打开文件
    #   - 路径:
    #       相对路径:'info.txt'
    #       绝对路径:'/Users/liuxiaowei/PycharmProjects/路飞全栈/day09/files/info.txt'
    #   - 模式
    #       rb,表示读取文件原始的二进制(r, 读 read;b, 二进制 binary;)
    # 1.打开文件
    file_object = open('files/info.txt', mode='rb')
    # 2.读取文件内容,并赋值给data
    data = file_object.read()
    # 3.关闭文件
    file_object.close()print(data) # b'alex-123\n\xe6\xad\xa6\xe6\xb2\x9b\xe9\xbd\x90-123'
    text = data.decode("utf-8")
    print(text)
    
    # 1.打开文件
    file_object = open('files/info.txt', mode='rt', encoding='utf-8')# 2.读取文件内容,并赋值给data
    data = file_object.read()# 3.关闭文件
    file_object.close()print(data)
    
  • 读图片等非文本内容文件。

    file_object = open('files/美女.png', mode='rb')
    data = file_object.read()
    file_object.close()print(data) # \x91\xf6\xf2\x83\x8aQFfv\x8b7\xcc\xed\xc3}\x7fT\x9d{.3.\xf1{\xe8\...
    

注意事项:

  • 路径

    • 相对路径,你的程序到底在哪里运行的?

    • 绝对路径

      # 1.打开文件
      file_object = open('/Users/liuxiaowei/PycharmProjects/路飞全栈/day09/files/info.txt', mode='rt', encoding='utf-8')
      # 2.读取文件内容,并赋值给data
      data = file_object.read()
      # 3.关闭文件
      file_object.close()
      

      windows系统中写绝对路径容易出问题:

      # file_object = open('C:\\new\\info.txt', mode='rt', encoding='utf-8')file_object = open(r'C:\new\info.txt', mode='rt', encoding='utf-8')
      data = file_object.read()
      file_object.close()
      print(data)
      
  • 读文件时,文件不存在程序会报错

    Traceback (most recent call last):File "/Users/liuxiaowei/PycharmProjects/路飞全栈/day09/2.读文件.py", line 2, in <module>file_object = open('infower.txt', mode='rt', encoding='utf-8')
    FileNotFoundError: [Errno 2] No such file or directory: 'infower.txt'
    
    # 判断路径是否存在?
    import osfile_path = "/Users/liuxiaowei/PycharmProjects/路飞全栈/day09/files/info.txt"
    exists = os.path.exists(file_path)
    if exists:# 1.打开文件file_object = open('files/info.txt', mode='rt', encoding='utf-8')# 2.读取文件内容,并赋值给datadata = file_object.read()# 3.关闭文件file_object.close()print(data)
    else:print("文件不存在")
    

1.2 写文件

  • 写文本文件

    # 1.打开文件
    # 路径:t1.txt
    # 模式:wb(要求写入的内容需要是字节类型)
    file_object = open("files/t1.txt", mode='wb')# 2.写入内容
    file_object.write(    "刘小伟".encode("utf-8")    )# 3.文件关闭
    file_object.close()
    
    file_object = open("files/t1.txt", mode='wt', encoding='utf-8')file_object.write("刘小伟")file_object.close()
    
  • 写图片等文件

    f1 = open('files/a1.png',mode='rb')
    content = f1.read()
    f1.close()f2 = open('a2.png',mode='wb')
    f2.write(content)
    f2.close()
    

基础案例

# 案例1:用户注册user = input("请输入用户名:")
pwd = input("请输入密码:")
data = f"{usre}-{pwd}"
file_object = open("files/info.txt", mode='wt', encoding='utf-8')
file_object.write(data)
file_object.close()
"""# 案例2:多用户注册
"""
# w写入文件,先清空文件;再在文件中写入内容。
file_object = open("files/info.txt", mode='wt', encoding='utf-8')
while True:user = input("请输入用户名:")if user.upper() == "Q":breakpwd = input("请输入密码:")data = f"{user}-{pwd}\n"file_object.write(data)
file_object.close()

注意事项:

  • 路径

    • 绝对路径
    • 相对路径
  • 文件不存在时,w模式会新建然后再写入内容;文件存在时,w模式会清空文件再写入内容。

1.3 文件打开模式

上文我们基于文件操作基本实现了读、写的功能,其中涉及的文件操作模式:rt、rb、wt、wb,其实在文件操作中还有其他的很多模式。

========= ===============================================================
Character Meaning
--------- ---------------------------------------------------------------
'r'       open for reading (default)
'w'       open for writing, truncating the file first
'x'       create a new file and open it for writing
'a'       open for writing, appending to the end of the file if it exists'b'       binary mode
't'       text mode (default)'+'       open a disk file for updating (reading and writing)The default mode is 'rt' (open for reading text).

关于文件的打开模式常见应用有:

  • 只读:rrtrb (用)

    • 存在,读
    • 不存在,报错
  • 只写:wwtwb(用)

    • 存在,清空再写
    • 不存在,创建再写
  • 只写:xxtxb

    • 存在,报错
    • 不存在,创建再写。
  • 只写:aatab【尾部追加】(用)

    • 存在,尾部追加。
    • 不存在,创建再写。
  • 读写

    • r+、rt+、rb+,默认光标位置:起始位置

      file_object = open('files/info.txt', mode='rt+')# 读取内容
      data = file_object.read()
      print(data)# 写入内容
      file_object.write("你好呀")file_object.close()
      
      file_object = open('files/info.txt', mode='rt+')# 写入内容
      file_object.write("alex")# 读取内容
      data = file_object.read()
      print(data)  # -123file_object.close()
      
    • w+、wt+、wb+,默认光标位置:起始位置(清空文件)

      # 读取内容
      data = file_object.read()
      print(data)# 写入内容
      file_object.write("你好呀")# 将光标位置重置起始
      file_object.seek(0)# 读取内容
      data = file_object.read()
      print(data)file_object.close()
      
    • x+、xt+、xb+,默认光标位置:起始位置(新文件)

    • a+、at+、ab+,默认光标位置:末尾

      file_object = open('files/info.txt', mode='at+')# 写入内容
      file_object.write("武沛齐")# 将光标位置重置起始
      file_object.seek(0)# 读取内容
      data = file_object.read()
      print(data)file_object.close()
      

多用户注册案例

while True:user = input("用户名:")if user.upper() == "Q":breakpwd = input("密码:")data = "{}-{}\n".format(user, pwd)file_object = open('files/account.txt', mode='a')file_object.write(data)file_object.close()
file_object = open('files/account.txt', mode='a')while True:user = input("用户名:")if user.upper() == "Q":breakpwd = input("密码:")data = "{}-{}\n".format(user, pwd)file_object.write(data)file_object.close()

1.4 常见功能

在上述对文件的操作中,我们只使用了write和read来对文件进行读写,其实在文件操作中还有很多其他的功能来辅助实现更好的读写文件的内容。

  • read,读

    • 读所有【常用】

      f = open('info.txt', mode='r',encoding='utf-8')
      data = f.read()
      f.close()
      
      f = open('info.txt', mode='rb')
      data = f.read()
      f.close()
      
    • 读n个字符(字节)【会用到】

      f = open('info.txt', mode='r', encoding='utf-8')
      # 读1个字符
      data = f.read(1)
      f.close()print(data) # 刘
      
      f = open('info.txt', mode='r',encoding='utf-8')# 读1个字符
      chunk1 = f.read(1)
      chunk2 = f.read(2)
      print(chunk1,chunk2)f.close()
      
      f = open('info.txt', mode='rb')# 读1个字节
      data = f.read(3)
      f.close()print(data, type(data))  # b'\xe6\xad\xa6' <class 'bytes'>
      
  • readline,读一行

    f = open('info.txt', mode='r', encoding='utf-8')v1 = f.readline()
    print(v1)v2 = f.readline()
    print(v2)f.close()
    
    f = open('info.txt', mode='r', encoding='utf-8')
    v1 = f.readline()
    print(v1)
    f.close()f = open('info.txt', mode='r', encoding='utf-8')
    v2 = f.readline()
    print(v2)
    f.close()
    
  • readlines,读所有行,每行作为列表的一个元素

    f = open('info.txt', mode='rb')data_list = f.readlines()f.close()print(data_list)
    
  • 循环,读大文件(readline加强版)【常见】

    f = open('info.txt', mode='r', encoding='utf-8')
    for line in f:print(line.strip())
    f.close()
    
  • write,写

    f = open('info.txt', mode='a',encoding='utf-8')
    f.write("刘小伟")
    f.close()
    
    f = open('info.txt', mode='ab')
    f.write( "刘小伟".encode("utf-8") )
    f.close()
    
  • flush,刷到硬盘

    f = open('info.txt', mode='a',encoding='utf-8')while True:# 不是写到了硬盘,而是写在缓冲区,系统会将缓冲区的内容刷到硬盘。f.write("刘小伟")f.flush()f.close()
    
    file_object = open('files/account.txt', mode='a')while True:user = input("用户名:")if user.upper() == "Q":breakpwd = input("密码:")data = "{}-{}\n".format(user, pwd)file_object.write(data)file_object.flush()file_object.close()
    
  • 移动光标位置(字节)

    f = open('info.txt', mode='r+', encoding='utf-8') # r+ 读写# 移动到指定字节的位置
    f.seek(3)
    f.write("刘小伟")f.close()
    

    注意:在a模式下,调用write在文件中写入内容时,永远只能将内容写入到尾部,不会写到光标的位置。

    f = open('files/info.txt', mode = 'r+', encoding='utf-8')f.seek(2)
    f.write('李小龙') # 先将光标移动到最后,再写数据, 可能出现乱码, 因为"utf-8"编码3个字节一个汉字,seek(2),移动# 第一个汉字的第2个字节
    f.close()
    
  • 获取当前光标位置

    # 读字符
    f = open('info.txt', mode='r', encoding='utf-8')p1 = f.tell()
    print(p1)  # 0f.read(3)  # 读3个中文字符 3*3=9字节 , 如果是英文字符,那么p2是3,因为每个英文字符一个字节p2 = f.tell()
    print(p2)  # 9f.close()
    
    # 读字节
    f = open('info.txt', mode='rb')p1 = f.tell()
    print(p1)  # 0f.read(3)  # 读3个字节p2 = f.tell()
    print(p2)  # 3f.close()
    

1.5 上下文管理

之前对文件进行操作时,每次都要打开和关闭文件,比较繁琐且容易忘记关闭文件。

以后再进行文件操作时,推荐大家使用with上下文管理,它可以自动实现关闭文件。

with open("xxxx.txt", mode='rb') as file_object:data = file_object.read()print(data)

在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:

with open("xxxx.txt", mode='rb') as f1, open("xxxx.txt", mode='rb') as f2:pass

练习题

  1. 补充代码:实现下载视频并保存到本地

    import requestsres = requests.get(url="https://weibo.com/newlogin?tabtype=weibo&gid=1028032222&openLoginLayer=0&url=https%3A%2F%2Fweibo.com%2F&layerid=4791081306164660",headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"}
    )# 视频的文件内容
    data = res.contentwith open('files/video.mp4', mode='wb') as f:f.write(data)
  2. 日志分析,计算某用户223.73.89.192访问次数。日志文件如下:access.log

    49.89.167.91 - - [17/Dec/2020:03:43:50 +0800] "GET /wiki/detail/3/40 HTTP/1.1" 301 0 "-" "Mozilla/5.0(Linux;Android 5.1.1;OPPO A33 Build/LMY47V;wv) AppleWebKit/537.36(KHTML,link Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36 LieBaoFast/4.51.3" "-"
    49.89.167.91 - - [17/Dec/2020:03:44:11 +0800] "GET /wiki/detail/3/40/ HTTP/1.1" 200 8033 "-" "Mozilla/5.0(Linux;Android 5.1.1;OPPO A33 Build/LMY47V;wv) AppleWebKit/537.36(KHTML,link Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36 LieBaoFast/4.51.3" "-"
    203.208.60.66 - - [17/Dec/2020:03:47:58 +0800] "GET /media/uploads/2019/11/17/pic/s1.png HTTP/1.1" 200 710728 "-" "Googlebot-Image/1.0" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /wiki/detail/3/40/ HTTP/1.1" 200 8033 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/stark/plugins/font-awesome/css/font-awesome.css HTTP/1.1" 200 37414 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/stark/plugins/bootstrap/css/bootstrap.css HTTP/1.1" 200 146010 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/web/css/commons.css HTTP/1.1" 200 3674 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/mdeditor/editormd/css/editormd.preview.css HTTP/1.1" 200 60230 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/stark/js/jquery-3.3.1.min.js HTTP/1.1" 200 86927 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/stark/plugins/bootstrap/js/bootstrap.min.js HTTP/1.1" 200 37045 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:26 +0800] "GET /static/mdeditor/editormd/lib/marked.min.js HTTP/1.1" 200 19608 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:27 +0800] "GET /static/mdeditor/editormd/lib/prettify.min.js HTTP/1.1" 200 17973 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:27 +0800] "GET /static/mdeditor/editormd/fonts/fontawesome-webfont.woff2?v=4.3.0 HTTP/1.1" 200 56780 "https://pythonav.com/static/mdeditor/editormd/css/editormd.preview.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:27 +0800] "GET /static/mdeditor/editormd/editormd.js HTTP/1.1" 200 163262 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:28 +0800] "GET /static/mdeditor/mdeditor-preview-init.js HTTP/1.1" 200 261 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:29 +0800] "GET /static/stark/plugins/font-awesome/fonts/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.1" 200 77160 "https://pythonav.com/static/stark/plugins/font-awesome/css/font-awesome.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    223.73.89.192 - - [17/Dec/2020:03:48:29 +0800] "GET /media/uploads/2019/02/22/Gobook/_book/ssl2.png HTTP/1.1" 200 203535 "https://pythonav.com/wiki/detail/3/40/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.60" "-"
    
    count = 0
    with open('files/access.log', mode='r', encoding='utf-8') as f:for line in f:  # 循环读取逐行数据data = line.strip()if not line.startswith('223.73.89.192'):continuecount += 1print(f"{'223.73.89.192'}访问次数{count}")
    
  3. 日志分析升级,计算所有用户的访问次数。

    user = {}
    with open('files/access.log', mode='r', encoding='utf-8') as f:for line in f:user_ip = line.split(' ')[0]if user_ip in user:user[user_ip] += 1else:user[user_ip] = 1for key, value in user.items():print(f'{key}访问次数{value}')
    
  4. 筛选出股票 当前价大于 20 的所有股票数据。

    股票代码,股票名称,当前价,涨跌额,涨跌幅,年初至今,成交量,成交额,换手率,市盈率(TTM),股息率,市值
    SH601778,N晶科,6.29,+1.92,+43.94%,+43.94%,259.66万,1625.52万,0.44%,22.32,-,173.95亿
    SH688566,吉贝尔,52.66,+6.96,+15.23%,+122.29%,1626.58万,8.09亿,42.29%,89.34,-,98.44亿
    SH688268,华特气体,88.80,+11.72,+15.20%,+102.51%,622.60万,5.13亿,22.87%,150.47,-,106.56亿
    SH600734,实达集团,2.60,+0.24,+10.17%,-61.71%,1340.27万,3391.14万,2.58%,亏损,0.00%,16.18亿
    SH900957,凌云B股,0.36,+0.033,+10.09%,-35.25%,119.15万,42.10万,0.65%,44.65,0.00%,1.26亿
    SZ000584,哈工智能,6.01,+0.55,+10.07%,-4.15%,2610.86万,1.53亿,4.36%,199.33,0.26%,36.86亿
    SH600599,熊猫金控,6.78,+0.62,+10.06%,-35.55%,599.64万,3900.23万,3.61%,亏损,0.00%,11.25亿
    SH600520,文一科技,8.21,+0.75,+10.05%,-24.05%,552.34万,4464.69万,3.49%,亏损,0.00%,13.01亿
    SH603682,锦和商业,11.73,+1.07,+10.04%,+48.29%,2746.63万,3.15亿,29.06%,29.62,-,55.42亿
    SZ300831,派瑞股份,12.27,+1.12,+10.04%,+208.29%,25.38万,311.41万,0.32%,60.59,-,39.26亿
    
    with open('files/stock.txt', mode='r') as f:for line in f:if line.startswith('股票代码'):continueprice = line.split(',')[2]if float(price) > 20:print(line.strip())
    
    with open('files/stock.txt', mode='r') as f:f.readline()  # 跳过第一行,因为readline()读完,光标落在最后for line in f:price = line.split(',')[2]if float(price) > 20:print(line.strip())
    
  5. 根据要求修改文件的内容,原文件内容如下:ha.conf

    global       log 127.0.0.1 local2daemonmaxconn 256log 127.0.0.1 local2 info
    defaultslog globalmode httptimeout connect 5000mstimeout client 50000mstimeout server 50000msoption  dontlognulllisten stats :8888stats enablestats uri       /adminstats auth      admin:1234frontend oldboy.orgbind 0.0.0.0:80option httplogoption httpcloseoption  forwardforlog globalacl www hdr_reg(host) -i www.luffycity.orguse_backend www.luffycity.com if wwwbackend www.luffycity.comserver 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
    ...
    

    请将文件中的luffycity修改为pythonav

    # 文件读到内存,再通过replace(适合小文件,不适用大文件)
    # 挨个位置读文件读内容,遇到luffcity将其替换为pythonav(不可取)
    # 同时打开两个文件,一个读,一个写(适合大小文件)
    with open('files/ha.conf', mode='r+') as f:for line in f:f.write(line.replace('luffcity', 'pythonav'))
    
    with open('files/ha.conf', mode='r', encoding='utf-8') as file_read, open('files/new_ha.conf', mode='w',                                                                                                                                                      encoding='utf-8') as file_write:for line in file_read:new_list = line.replace('luffycity', 'pythonav')file_write.write(new_list)# 重命名
    import shutil
    shutil.move('files/new_ha.conf', 'files/ha.conf')
    

2. csv格式文件

逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。

对于这种格式的数据,我们需要利用open函数来读取文件并根据逗号分隔的特点来进行处理。

股票代码,股票名称,当前价,涨跌额,涨跌幅,年初至今
SH601778,N晶科,6.29,+1.92,-43.94%,+43.94%
SH688566,吉贝尔,52.66,+6.96,+15.23%,+122.29%
...

练习题案例:下载文档中的所有图片且以用户名为图片名称存储。

ID,用户名,头像
26044585,Hush,https://hbimg.huabanimg.com/51d46dc32abe7ac7f83b94c67bb88cacc46869954f478-aP4Q3V
19318369,柒十一,https://hbimg.huabanimg.com/703fdb063bdc37b11033ef794f9b3a7adfa01fd21a6d1-wTFbnO
15529690,Law344,https://hbimg.huabanimg.com/b438d8c61ed2abf50ca94e00f257ca7a223e3b364b471-xrzoQd
18311394,Jennah·,https://hbimg.huabanimg.com/4edba1ed6a71797f52355aa1de5af961b85bf824cb71-px1nZz
18009711,可洛爱画画,https://hbimg.huabanimg.com/03331ef39b5c7687f5cc47dbcbafd974403c962ae88ce-Co8AUI
30574436,花姑凉~,https://hbimg.huabanimg.com/2f5b657edb9497ff8c41132e18000edb082d158c2404-8rYHbw
17740339,小巫師,https://hbimg.huabanimg.com/dbc6fd49f1915545cc42c1a1492a418dbaebd2c21bb9-9aDqgl
18741964,桐末tonmo,https://hbimg.huabanimg.com/b60cee303f62aaa592292f45a1ed8d5be9873b2ed5c-gAJehO
30535005,TANGZHIQI,https://hbimg.huabanimg.com/bbd08ee168d54665bf9b07899a5c4a4d6bc1eb8af77a4-8Gz3K1
31078743,你的老杨,https://hbimg.huabanimg.com/c46fbc3c9a01db37b8e786cbd7174bbd475e4cda220f4-F1u7MX
25519376,尺尺寸,https://hbimg.huabanimg.com/ee29ee198efb98f970e3dc2b24c40d89bfb6f911126b6-KGvKes
21113978,C-CLong,https://hbimg.huabanimg.com/7fa6b2a0d570e67246b34840a87d57c16a875dba9100-SXsSeY
24674102,szaa,https://hbimg.huabanimg.com/0716687b0df93e8c3a8e0925b6d2e4135449cd27597c4-gWdv24
30508507,爱起床的小灰灰,https://hbimg.huabanimg.com/4eafdbfa21b2f300a7becd8863f948e5e92ef789b5a5-1ozTKq
12593664,yokozen,https://hbimg.huabanimg.com/cd07bbaf052b752ed5c287602404ea719d7dd8161321b-cJtHss
16899164,一阵疯,https://hbimg.huabanimg.com/0940b557b28892658c3bcaf52f5ba8dc8402100e130b2-G966Uz
847937,卩丬My㊊伴er彎,https://hbimg.huabanimg.com/e2d6bb5bc8498c6f607492a8f96164aa2366b104e7a-kWaH68
31010628,慢慢即漫漫,https://hbimg.huabanimg.com/c4fb6718907a22f202e8dd14d52f0c369685e59cfea7-82FdsK
13438168,海贼玩跑跑,https://hbimg.huabanimg.com/1edae3ce6fe0f6e95b67b4f8b57c4cebf19c501b397e-BXwiW6
28593155,源稚生,https://hbimg.huabanimg.com/626cfd89ca4c10e6f875f3dfe1005331e4c0fd7fd429-9SeJeQ
28201821,合伙哼哼,https://hbimg.huabanimg.com/f59d4780531aa1892b80e0ec94d4ec78dcba08ff18c416-769X6a
28255146,漫步AAA,https://hbimg.huabanimg.com/3c034c520594e38353a039d7e7a5fd5e74fb53eb1086-KnpLaL
30537613,配䦹,https://hbimg.huabanimg.com/efd81d22c1b1a2de77a0e0d8e853282b83b6bbc590fd-y3d4GJ
22665880,日后必火,https://hbimg.huabanimg.com/69f0f959979a4fada9e9e55f565989544be88164d2b-INWbaF
16748980,keer521521,https://hbimg.huabanimg.com/654953460733026a7ef6e101404055627ad51784a95c-B6OFs4
30536510,“西辞”,https://hbimg.huabanimg.com/61cfffca6b2507bf51a507e8319d68a8b8c3a96968f-6IvMSk
30986577,艺成背锅王,https://hbimg.huabanimg.com/c381ecc43d6c69758a86a30ebf72976906ae6c53291f9-9zroHF
26409800,CsysADk7,https://hbimg.huabanimg.com/bf1d22092c2070d68ade012c588f2e410caaab1f58051-ahlgLm
30469116,18啊全阿,https://hbimg.huabanimg.com/654953460733026a7ef6e101404055627ad51784a95c-B6OFs4
15514336,W/小哥,https://hbimg.huabanimg.com/a30f5967fc0acf81421dd49650397de63c105b9ead1c-nVRrNl
17473505,椿の花,https://hbimg.huabanimg.com/0e38d810e5a24f91ebb251fd3aaaed8bb37655b14844c-pgNJBP
19165177,っ思忆゜♪,https://hbimg.huabanimg.com/4815ea0e4905d0f3bb82a654b481811dadbfe5ce2673-vMVr0B
16059616,格林熊丶,https://hbimg.huabanimg.com/8760a2b08d87e6ed4b7a9715b1a668176dbf84fec5b-jx14tZ
30734152,sCWVkJDG,https://hbimg.huabanimg.com/f31a5305d1b8717bbfb897723f267d316e58e7b7dc40-GD3e22
24019677,虚无本心,https://hbimg.huabanimg.com/6fdfa9834abe362e978b517275b06e7f0d5926aa650-N1xCXE
16670283,Y-雨后天空,https://hbimg.huabanimg.com/a3bbb0045b536fc27a6d2effa64a0d43f9f5193c177f-I2vHaI
21512483,汤姆2,https://hbimg.huabanimg.com/98cc50a61a7cc9b49a8af754ffb26bd15764a82f1133-AkiU7D
16441049,笑潇啸逍小鱼,https://hbimg.huabanimg.com/ae8a70cd85aff3a8587ff6578d5cf7620f3691df13e46-lmrIi9
24795603,⁢⁢⁢⁢⁢v,https://hbimg.huabanimg.com/a7183cc3a933aa129d7b3230bf1378fd8f5857846cc5-3tDtx3
29819152,妮玛士珍多,https://hbimg.huabanimg.com/ca4ecb573bf1ff0415c7a873d64470dedc465ea1213c6-RAkArS
19101282,陈勇敢❤,https://hbimg.huabanimg.com/ab6d04ebaff3176e3570139a65155856871241b58bc6-Qklj2E
28337572,爱意随风散,https://hbimg.huabanimg.com/117ad8b6eeda57a562ac6ab2861111a793ca3d1d5543-SjWlk2
17342758,幸运instant,https://hbimg.huabanimg.com/72b5f9042ec297ae57b83431123bc1c066cca90fa23-3MoJNj
18483372,Beau染,https://hbimg.huabanimg.com/077115cb622b1ff3907ec6932e1b575393d5aae720487-d1cdT9
22127102,栽花的小蜻蜓,https://hbimg.huabanimg.com/6c3cbf9f27e17898083186fc51985e43269018cc1e1df-QfOIBG
13802024,LoveHsu,https://hbimg.huabanimg.com/f720a15f8b49b86a7c1ee4951263a8dbecfe3e43d2d-GPEauV
22558931,白驹过隙丶梨花泪う,https://hbimg.huabanimg.com/e49e1341dfe5144da5c71bd15f1052ef07ba7a0e1296b-jfyfDJ
11762339,cojoy,https://hbimg.huabanimg.com/5b27f876d5d391e7c4889bc5e8ba214419eb72b56822-83gYmB
30711623,雪碧学长呀,https://hbimg.huabanimg.com/2c288a1535048b05537ba523b3fc9eacc1e81273212d1-nr8M4t
18906718,西霸王,https://hbimg.huabanimg.com/7b02ad5e01bd8c0a29817e362814666a7800831c154a6-AvBDaG
31037856,邵阳的小哥哥,https://hbimg.huabanimg.com/654953460733026a7ef6e101404055627ad51784a95c-B6OFs4
26830711,稳健谭,https://hbimg.huabanimg.com/51547ade3f0aef134e8d268cfd4ad61110925aefec8a-NKPEYX
import requests
import oswith open('files/mv.csv', mode='r', encoding='utf-8') as file_read:file_read.readline() # 跳过第一行for line in file_read:content = line.split(',')file_name = content[1]file_url = content[2].strip()res = requests.get(url=file_url,headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"})if not os.path.exists('images'):os.mkdir('images')with open(f'images/{file_name}.png', mode='wb') as fw:fw.write(res.content)

3. ini格式文件

ini文件是Initialization File的缩写,平时用于存储软件的的配置文件。例如:MySQL数据库的配置文件

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=py-mysql-bin
character-set-server=utf8
collation-server=utf8_general_ci
log-error=/var/log/mysqld.log
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid[client]
default-character-set=utf8

这种格式是可以直接使用open来出来,考虑到自己处理比较麻烦,所以Python为我们提供了更为方便的方式。

  • 读取所有节点

    import configparserconfig = configparser.ConfigParser()
    config.read('files/my.ini', encoding='utf-8')# 获取所有的节点
    result = config.sections()
    print(result)>>> 输出
    ['mysqld', 'mysqld_safe', 'client']
    
  • 读取节点下的键值

    import configparserconfig = configparser.ConfigParser()
    config.read('files/my.ini', encoding='utf-8')# 获取所有的节点
    result = config.sections()
    text = config.items(result[1])for data in text:print('='.join(list(data)))for key, value in text:  # 因为text列表里的元素是元组,所以key, value 对应print(key, value)
    
  • 读取节点下值(根据 节点+键 )

    result = config.get('mysqld', 'collation-server')  # 'mysqld' -> 节点,'collation-server' -> 键
    print(result)
    
  • 检查、删除、添加节点

    # 添加一个节点
    config.add_section('group')
    config.write(open('files/my.ini', mode='w', encoding='utf-8'))   # 写入到文件对象# 添加节点下面到键值
    if not config.has_section('group'):config.add_section('group')
    config.set('group', 'name', 'liuxiaowei')
    config.set('client','name', 'liuxiaowei')
    config.write(open('files/my.ini', mode='w', encoding='utf-8'))   # 写入到文件对象# 删除节点
    if config.has_section('group'):config.remove_section('group')config.write(open('files/my.ini', mode='w', encoding='utf-8'))# 删除节点下面的内容
    if config.has_section('client'):config.remove_section('client')
    config.remove_option('mysqld', 'datadir')
    config.write(open('files/my.ini', mode='w', encoding='utf-8'))
    

4. XML格式文件

可扩展标记语言,是一种简单的数据存储语言,XML 被设计用来传输和存储数据。

  • 存储,可用来存放配置文件,例如:java的配置文件。
  • 传输,网络传输时以这种格式存在,例如:早期ajax传输的数据、soap协议等。
<data><country name="Liechtenstein"><rank updated="yes">2</rank><year>2023</year><gdppc>141100</gdppc><neighbor direction="E" name="Austria" /><neighbor direction="W" name="Switzerland" /></country><country name="Singapore"><rank updated="yes">5</rank><year>2026</year><gdppc>59900</gdppc><neighbor direction="N" name="Malaysia" /></country><country name="Panama"><rank updated="yes">69</rank><year>2026</year><gdppc>13600</gdppc><neighbor direction="W" name="Costa Rica" /><neighbor direction="E" name="Colombia" /></country>
</data>

微信官方文档:公众号

4.1 读取文件和内容

from xml.etree import ElementTree as ET# ET去打开xml文件
tree = ET.parse("files/xo.xml")# 获取根标签
root = tree.getroot()print(root) # <Element 'data' at 0x7f94e02763b0>
from xml.etree import ElementTree as ETcontent = """
<data><country name="Liechtenstein"><rank updated="yes">2</rank><year>2023</year><gdppc>141100</gdppc><neighbor direction="E" name="Austria" /><neighbor direction="W" name="Switzerland" /></country><country name="Panama"><rank updated="yes">69</rank><year>2026</year><gdppc>13600</gdppc><neighbor direction="W" name="Costa Rica" /><neighbor direction="E" name="Colombia" /></country>
</data>
"""root = ET.XML(content) # xml以字符串形式存在的时候
print(root)  # <Element 'data' at 0x7fdaa019cea0>

4.2 读取节点数据

from xml.etree import ElementTree as ETcontent = """
<data><country name="Liechtenstein" id="999" ><rank>2</rank><year>2023</year><gdppc>141100</gdppc><neighbor direction="E" name="Austria" /><neighbor direction="W" name="Switzerland" /></country><country name="Panama"><rank>69</rank><year>2026</year><gdppc>13600</gdppc><neighbor direction="W" name="Costa Rica" /><neighbor direction="E" name="Colombia" /></country>
</data>
"""root = ET.XML(content)
for child in root:print(child.tag, child.attrib)for node in child:print(node.tag, node.attrib, node.text)
from xml.etree import ElementTree as ETcontent = """
<data><country name="Liechtenstein" id="999" ><rank>2</rank><year>2023</year><gdppc>141100</gdppc><neighbor direction="E" name="Austria" /><neighbor direction="W" name="Switzerland" /></country><country name="Panama"><rank>69</rank><year>2026</year><gdppc>13600</gdppc><neighbor direction="W" name="Costa Rica" /><neighbor direction="E" name="Colombia" /></country>
</data>
"""
root = ET.XML(content)for child in root.iter('year'): # 去data标签子子孙孙里找所有的year标签print(child.tag, child.text)
from xml.etree import ElementTree as ETcontent = """
<data><country name="Liechtenstein" id="999" ><rank>2</rank><year>2023</year><gdppc>141100</gdppc><neighbor direction="E" name="Austria" /><neighbor direction="W" name="Switzerland" /></country><country name="Panama"><rank>69</rank><year>2026</year><gdppc>13600</gdppc><neighbor direction="W" name="Costa Rica" /><neighbor direction="E" name="Colombia" /></country>
</data>
"""# 获取根标签 data
root = ET.XML(content)v1 = root.findall('country')
print(v1)  # [<Element 'country' at 0x7fbdd2df7cc0>, <Element 'country' at 0x7fbdd2df7ea0>, <Element 'country' at 0x7fbdd2dfa090>]# 获取指定标签的内容
v1 = root.findall('country')
for rank in v1:print(rank.find('rank').text)v2 = root.find('country').find('rank')
print(v2.text)
from xml.etree import ElementTree as ETcontent = """
<data><country name="Liechtenstein" id="999" ><rank>2</rank><year>2023</year><gdppc>141100</gdppc><neighbor direction="E" name="Austria" /><neighbor direction="W" name="Switzerland" /></country><country name="Panama"><rank>69</rank><year>2026</year><gdppc>13600</gdppc><neighbor direction="W" name="Costa Rica" /><neighbor direction="E" name="Colombia" /></country>
</data>
"""# 获取根标签 data
root = ET.XML(content)country_object = root.find("country")
print(country_object.tag, country_object.attrib)
gdppc_object = country_object.find("gdppc")
print(gdppc_object.tag,gdppc_object.attrib,gdppc_object.text)

4.3 修改和删除节点

from xml.etree import ElementTree as ETcontent = """
<data><country name="Liechtenstein"><rank>2</rank><year>2023</year><gdppc>141100</gdppc><neighbor direction="E" name="Austria" /><neighbor direction="W" name="Switzerland" /></country><country name="Panama"><rank>69</rank><year>2026</year><gdppc>13600</gdppc><neighbor direction="W" name="Costa Rica" /><neighbor direction="E" name="Colombia" /></country>
</data>
"""root = ET.XML(content)# 修改节点内容和属性
rank = root.find('country').find('rank')
print(rank.text)
rank.text = "999"  # 修改内容
rank.set('updated', '2022-07-14') # 设置一个属性
print(rank.text, rank.attrib)
############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("files/new.xml", encoding='utf-8')# 删除节点
root.remove( root.find('country') )
print(root.findall('country'))############ 保存文件 ############
tree = ET.ElementTree(root)
tree.write("files/new.xml", encoding='utf-8')

4.4 构建文档

<home><son name="儿1"><grandson name="儿11"></grandson><grandson name="儿12"></grandson></son><son name="儿2"></son>
</home>
from xml.etree import ElementTree as ET# 创建根节点
root = ET.Element('home')# 创建节点大儿子
son1 = ET.Element('son', {'name': '儿1'})
# 创建节点小儿子
son2 = ET.Element('son', {'name': '儿2'})# 在大儿子中创建两个孙子
grandson1 = ET.Element('grandson', {'name': '儿11'})
grandson2 = ET.Element('grandson', {'name': '儿12'})
son1.append(grandson1)
son1.append(grandson2)# 把儿子添加到根节点中
root.append(son1)
root.append(son2)tree = ET.ElementTree(root)
tree.write('file/oooo.xml', encoding='utf-8', short_empty_elements=False) # 是否用短标签
<famliy><son name="儿1"><grandson name="儿11"></grandson><grandson name="儿12"></grandson></son><son name="儿2"></son>
</famliy>
from xml.element import ElementTree as ET# 创建根节点
root = ET.Element('family')# 创建大儿子
son1 = root.makeelement('son', {'name': '儿1'})# 创建小儿子
son2 = root.makeelement('son', {'name': '儿2'})# 在大儿子中创建两个孙子
grandson1 = son1.makeelement('grandson', {'name': '儿11'})
grandson2 = son1.makeelemnt('grandson', {'name': '儿12'})son1.append(grandson1)
son1.append(grandson2)# 把儿子添加到根节点中
root.append(son1)
root.append(son2)tree = ET.ElementTree(root)
tree.write('files/oooo.xml', encoding='utf-8')
<famliy><son name="儿1"><age name="儿11">孙子</age></son><son name="儿2"></son>
</famliy>
from xml.etree import ElementTree as ET# 创建根节点
root = ET.Element("famliy")# 创建节点大儿子
son1 = ET.SubElement(root, "son", attrib={'name': '儿1'})
# 创建小儿子
son2 = ET.SubElement(root, "son", attrib={"name": "儿2"})# 在大儿子中创建一个孙子
grandson1 = ET.SubElement(son1, "age", attrib={'name': '儿11'})
grandson1.text = '孙子'et = ET.ElementTree(root)  #生成文档对象
et.write("test.xml", encoding="utf-8")
<user><![CDATA[你好呀]]</user>
from xml.etree import ElementTree as ET# 创建根节点
root = ET.Element("user")
root.text = "<![CDATA[你好呀]]"et = ET.ElementTree(root)  # 生成文档对象
et.write("test.xml", encoding="utf-8")

案例:

content = """<xml><ToUserName><![CDATA[gh_7f083739789a]]></ToUserName><FromUserName><![CDATA[oia2TjuEGTNoeX76QEjQNrcURxG8]]></FromUserName><CreateTime>1395658920</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[TEMPLATESENDJOBFINISH]]></Event><MsgID>200163836</MsgID><Status><![CDATA[success]]></Status>
</xml>"""from xml.etree import ElementTree as ETinfo = {}
root = ET.XML(content)
for node in root:# print(node.tag,node.text)info[node.tag] = node.text
print(info)

5. Excel格式文件

Python内部未提供处理Excel文件的功能,想要在Python中操作Excel需要按照第三方的模块。

pip install openpyxl

此模块中集成了Python操作Excel的相关功能,接下来我们就需要去学习该模块提供的相关功能即可。

5.1 读Excel

  • 读sheet

    from openpyxl import load_workbookwb = load_workbook("files/p1.xlsx")# sheet相关操作# 1.获取excel文件中的所有sheet名称
    """
    print(wb.sheetnames) # ['数据导出', '用户列表', 'Sheet1', 'Sheet2']
    """# 2.选择sheet,基于sheet名称
    """
    sheet = wb["数据导出"]
    cell = sheet.cell(1, 2)
    print(cell.value)
    """# 3.选择sheet,基于索引位置
    """
    sheet = wb.worksheets[0]
    cell = sheet.cell(1,2)
    print(cell.value)
    """# 4.循环所有的sheet
    """
    for name in wb.sheetnames:sheet = wb[name]cell = sheet.cell(1, 1)print(cell.value)
    """
    """
    for sheet in wb.worksheets:cell = sheet.cell(1, 1)print(cell.value)
    """
    """
    for sheet in wb:cell = sheet.cell(1, 1)print(cell.value)
    """
    
  • 读sheet中单元格的数据

    from openpyxl import load_workbookwb = load_workbook("files/p1.xlsx")
    sheet = wb.worksheets[0]# 1.获取第N行第N列的单元格(位置是从1开始)
    """
    cell = sheet.cell(1, 1)print(cell.value)
    print(cell.style)
    print(cell.font)
    print(cell.alignment)
    """# 2.获取某个单元格
    """
    c1 = sheet["A2"]
    print(c1.value)c2 = sheet['D4']
    print(c2.value)
    """# 3.第N行所有的单元格
    """
    for cell in sheet[1]:print(cell.value)
    """# 4.所有行的数据(获取某一列数据)
    """
    for row in sheet.rows:print(row[0].value, row[1].value)
    """# 5.获取所有列的数据
    """
    for col in sheet.columns:print(col[1].value)
    """
    
  • 读合并的单元格

    from openpyxl import load_workbookwb = load_workbook('files/p1.xlsx')sheet = wb.worksheets[2]# 获取第N行第N列第单元格(位置从1开始)
    c1 = sheet.cell(1, 1)print(c1)  # <Cell 'Sheet1'.A1>
    print(c1.value)  # 用户信息c2 = sheet.cell(1, 2)
    print(c2)  # <MergedCell 'Sheet1'.B1>
    print(c2.value) # None
    
    from openpyxl import load_workbookwb = load_workbook('files/p1.xlsx')
    sheet = wb.sheets[2]
    for row in sheet.rows:print(row)>>>
    (<Cell 'Sheet1'.A1>, <MergedCell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>)
    (<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>)
    (<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>)
    (<MergedCell 'Sheet1'.A4>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.C4>)
    (<Cell 'Sheet1'.A5>, <Cell 'Sheet1'.B5>, <Cell 'Sheet1'.C5>)
    

5.2 写Excel

在Excel中想要写文件,大致要分为在:

  • 原Excel文件基础上写内容。

    from openpyxl import load_workbookwb = load_workbook('files/p1.xlsx')
    sheet = wb.worksheets[0]# 找到单元格,并修改单元格的内容
    cell = sheet.cell(1, 1)
    cell.value = "新的开始"# 将excel文件保存到p2.xlsx文件中
    wb.save("files/p2.xlsx")
    
  • 新创建Excel文件写内容。

    from openpyxl import workbook# 创建excel且默认会创建一个sheet(名称为Sheet)
    wb = workbook.Workbook()sheet = wb.worksheets[0] # 或 sheet = wb["Sheet"]# 找到单元格,并修改单元格的内容
    cell = sheet.cell(1, 1)
    cell.value = "新的开始"# 将excel文件保存到p2.xlsx文件中
    wb.save("files/p2.xlsx")
    

5.2.1 修改sheet名称

from openpyxl import workbook
wb = workbook.Workbook() # Sheetsheet = wb.worksheets[0]
sheet.title = "数据集"
wb.save("p2.xlsx")

5.2.2 创建sheet并设置sheet颜色

from openpyxl import workbook
wb = workbook.Workbook() # Sheetsheet = wb.create_sheet("工作计划", 0)
sheet.sheet_properties.tabColor = "1072BA"
wb.save("p2.xlsx")

5.2.3 默认打开的sheet

from openpyxl import workbook
wb = workbook.Workbook() # Sheetsheet = wb.create_sheet("工作计划", 0)
sheet.sheet_properties.tabColor = "1072BA"
wb.save("p2.xlsx")

5.2.4 拷贝sheet

from openpyxl import workbook
wb = workbook.Workbook() # Sheetsheet = wb.create_sheet("工作计划")
sheet.sheet_properties.tabColor = "1072BA"new_sheet = wb.copy_worksheet(wb["Sheet"])
new_sheet.title = "新的计划"
wb.save("p2.xlsx")

5.2.5 删除sheet

from openpyxl import workbook
wb = workbook.Workbook() # Sheetdel wb["用户列表"]
wb.save('files/p2.xlsx')

5.3 对单元格及属性操作

5.3.1 获取某个单元格,修改值

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillwb = load_workbook('files/p1.xlsx')sheet = wb.worksheets[1]

5.3.2 获取某个单元格,修改值

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillsheet["B3"] = "Alex"
wb.save("p2.xlsx")

5.3.3 获取某些单元格,修改值

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillcell_list = sheet["B2":"C3"]
for row in cell_list:for cell in row:cell.value = "新的值"
wb.save("p2.xlsx")

5.3.4 对齐方式

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillcell = sheet.cell(1, 1)# horizontal,水平方向对齐方式:"general", "left", "center", "right", "fill", "justify", "centerContinuous", "distributed"
# vertical,垂直方向对齐方式:"top", "center", "bottom", "justify", "distributed"
# text_rotation,旋转角度。
# wrap_text,是否自动换行。
cell.alignment = Alignment(horizontal='center', vertical='distributed', text_rotation=45, wrap_text=True)
wb.save("p2.xlsx")
"""

5.3.5 边框

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFill# side的style有如下:dashDot','dashDotDot', 'dashed','dotted','double','hair', 'medium', 'mediumDashDot', 'mediumDashDotDot','mediumDashed', 'slantDashDot', 'thick', 'thin'cell = sheet.cell(9, 2)
cell.border = Border(top=Side(style="thin", color="FFB6C1"), bottom=Side(style="dashed", color="FFB6C1"),left=Side(style="dashed", color="FFB6C1"),right=Side(style="dashed", color="9932CC"),diagonal=Side(style="thin", color="483D8B"),  # 对角线diagonalUp=True,  # 左下 ~ 右上diagonalDown=True  # 左上 ~ 右下
)
wb.save("p2.xlsx")

5.3.6 字体

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillcell = sheet.cell(5, 1)
cell.font = Font(name="微软雅黑", size=45, color="ff0000", underline="single")
wb.save("p2.xlsx")

5.3.6 背景色

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillcell = sheet.cell(5, 3)
cell.fill = PatternFill("solid", fgColor="99ccff")
wb.save("p2.xlsx")

5.3.7 渐变背景色

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillcell = sheet.cell(5, 5)
cell.fill = GradientFill("linear", stop=("FFFFFF", "99ccff", "000000"))
wb.save("p2.xlsx")

5.3.8 宽高(索引从1开始)

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillsheet.row_dimensions[1].height = 50
sheet.column_dimensions["E"].width = 100
wb.save("p2.xlsx")

5.3.9 合并单元格

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillsheet.merge_cells("B2:D8")
sheet.merge_cells(start_row=15, start_column=3, end_row=18, end_column=8)
wb.save("p2.xlsx")# 取消合并单元格
sheet.unmerge_cells("B2:D8")
wb.save("p2.xlsx")

5.3.10 写入公式

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillsheet = wb.worksheets[3]
sheet["D1"] = "合计"
sheet["D2"] = "=B2*C2"
wb.save("p2.xlsx")sheet = wb.worksheets[3]
sheet["D3"] = "=SUM(B3,C3)"
wb.save("p2.xlsx")

5.3.11 删除

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFill# idx,要删除的索引位置
# amount,从索引位置开始要删除的个数(默认为1)
sheet.delete_rows(idx=1, amount=20)
sheet.delete_cols(idx=1, amount=3)
wb.save("p2.xlsx")

5.3.12 插入

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillsheet.insert_rows(idx=5, amount=10)
sheet.insert_cols(idx=3, amount=2)
wb.save("p2.xlsx")

5.3.13 循环写内容

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillsheet = wb["Sheet"]
cell_range = sheet['A1:C2']
for row in cell_range:for cell in row:cell.value = "xx"for row in sheet.iter_rows(min_row=5, min_col=1, max_col=7, max_row=10):for cell in row:cell.value = "oo"
wb.save("p2.xlsx")

5.3.14 移动

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFill# 将H2:J10范围的数据,向右移动15个位置、向上移动1个位置
sheet.move_range("H2:J10",rows=1, cols=15)
wb.save("p2.xlsx")sheet = wb.worksheets[3]
sheet["D1"] = "合计"
sheet["D2"] = "=B2*C2"
sheet["D3"] = "=SUM(B3,C3)"
sheet.move_range("B1:D3",cols=10, translate=True) # 自动翻译公式
wb.save("p2.xlsx")

5.3.15 打印区域

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillsheet.print_area = "A1:D200"
wb.save("p2.xlsx")

5.3.16 打印时,每个页面的固定表头

from openpyxl import load_workbook
from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFillsheet.print_title_cols = "A:D"
sheet.print_title_rows = "1:3"
wb.save("p2.xlsx")

Python对csv、ini、xml、excel等格式文件操作用例相关推荐

  1. Unity 工具类 之 Excel 转换为 json、csv、xml、lua格式

    Unity 工具类 之 Excel 转换为 json.csv.xml.csv 格式 目录 Unity 工具类 之 Excel 转换为 json.csv.xml.csv 格式 一.介绍 二.操作原理 三 ...

  2. python将源代码转换成在html可显示的格式,Python实现将HTML转换成doc格式文件的方法示例...

    Python实现将HTML转换成doc格式文件的方法示例 来源:中文源码网    浏览: 次    日期:2018年9月2日 [下载文档:  Python实现将HTML转换成doc格式文件的方法示例. ...

  3. Pandas载入数据(csv, JSON,XML,Excel,二进制数据,网页数据,数据库数据)

    数据载入,存储及文件格式 这里用到的样本数据git地址:https://github.com/wesm/pydata-book 访问数据是使用各类工具所必须的第一步(获取数据).这里重点讨论使用pan ...

  4. python将csv一行保存一个txt_Python读写文件(csv、txt、excel)

    大家做在数据解决的时候,一定难免会与文件交互,那么对于指定的文件类型,我们如何操作呢? 1.python读写csv文件import csv #python2可以用file替代openwith open ...

  5. r语言导出文件为xlxs_R语言学习——R读取txt、csv、xls和xlsx格式文件

    最近项目中运用到了R读取文件数据,所以把相关好用的.经过验证的方法总结了一下,有效避免下次入坑. 1. R读取txt文件 使用R读取txt文件直接使用read.table()方法进行读取即可,不需要加 ...

  6. python转化成excel_python转换excel成py文件

    python转换excel成py文件 文件结构如下: originExcelFolder放用来转换的excel文件. targetPyFolder用来存放最后生产的py文件. setting.py用来 ...

  7. python怎在excel-Python操作Excel之xlsx文件

    前言 之前处理excel的读写时用的是xlrd/xlwt,但是这两个库有个缺点就是只对xls的格式处理的比较好,对以xlsx结尾的格式就不行了.由于现在大家使用的都是最新版本的office,excel ...

  8. python中的函数wavfile_Python--读取wav格式文件

    Python--读取wav格式文件 (2013-05-28 06:56:22) 标签: 格式文件 读取 杂谈 1.import wave 用于读写wav文件 它提供了一个方便的WAV#26684;式接 ...

  9. python codecs.open使用_python codecs.open()及文件操做

    文件读取:数组 codecs.open(filepath,method,encoding)安全 filepath--文件路径函数 method--打开方式,r为读,w为写,rw为读写编码 encodi ...

最新文章

  1. 《Sibelius 脚本程序设计》连载(二十六) - 2.13 utils库中的函数
  2. opencv 仪表数字切割
  3. 使用 ZwUnmapViewOfSection 卸载并替换内存镜像
  4. Zabbix-3.0.0 安装Graphtree
  5. 第三十五期:当我们在讨论CQRS时,我们在讨论些神马?
  6. java ajax 联动菜单_java结合jQuery.ajax实现左右菜单联动刷新列表内容
  7. 华为手表表盘的数字什么意思_手表的陀飞轮、月相、逆跳都是什么意思?
  8. Python基础---OS模块 (二)
  9. Windows 下利用cWrsync同步
  10. uniapp ios 沙盒测试支付(苹果支付)
  11. 实验三 图像空间域平滑与锐化(Python实现)
  12. Venmo、Bakkt、MoneyGram、Uphold的前高管加入Roxe全球支付网络
  13. 如何让机器产生意识之意识具象化
  14. 讯飞AIUI智能机器人2
  15. cdrx8如何批量导出jpg_CDR怎么批量导出图片
  16. 【vue】在vue,vue cli中拼接字符串,拼接图片链接
  17. 国科大抢课避坑+选课指南+教务系统操作
  18. python窗口大小动态变化_如何在tkinter中动态调整窗口大小以“自动调整”其内容?...
  19. Java产生的历史与现状
  20. 汤晓丹的第四版计算机操作系统--第十章总结概述

热门文章

  1. Cadence Other格式网表导出与导入
  2. android支持gif图片格式,Android 支持Gif动态图 的imagview
  3. 【MySQL学习】事务管理
  4. Android手机基本教程
  5. 【Linux】进程状态(阻塞、挂起、僵尸进程)
  6. CentOS 7 root 密码破解及 grub2 加密
  7. 关于Stream()和Collectors.joining()字符串连接器
  8. 互联网用户公众账号信息服务管理规定
  9. 阿甘修理机器人cd_剑网3遗失的美好现在选什么东西好?
  10. NDIS中间层的驱动包截获技术教程