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')