Python手记

Python将UTF-8 w/ BOM转换为UTF-8 w/o BOM

按UTF-8 w/ BOM读入文件,再用UTF-8存储。如果源文件不是UTF-8 w/ BOM或UTF-8 w/o BOM编码,会抛出异常并打印文件全路径,但程序不会终止。

import os

def convert(d):
    for f in os.listdir(d):
        full = os.path.join(d, f)
        if os.path.isfile(full):
            try:
                s = open(full, mode='r', encoding='utf-8-sig').read()
                open(full, mode='w', encoding='utf-8').write(s)
            except Exception as e:
                print(f'ERROR: Non UTF-8 encoded file found: {full}')
        if os.path.isdir(full):
            convert(full)

解决ssl1错误:

错误信息:ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1131)

from urllib.request import urlopen
import ssl
response = urlopen('https://somedomain.co', context=ssl._create_unverified_context())

Python生成图片

from PIL import Image, ImageDraw, ImageFont

W, H = 480, 270
img = Image.new('RGB', (W, H), (58, 128, 168))
draw = ImageDraw.Draw(img)
font_size = 136
msg = '#Node.js'
while True:
	font = ImageFont.truetype('STSONG.TTF', size=font_size)
	_, _, w, h = draw.textbox((0, 0), msg, font=font)
	if W - w >= 100:
		break
	font_size -= 1
draw.text(xy=((W - w) / 2, (H - h) / 2), text=msg, font=font)
img.save('temp.png')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值