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')
本文介绍了如何使用Python处理UTF-8带BOM头的文件,将其转换为标准UTF-8格式,并处理遇到的非UTF-8编码文件,确保文件正确读写。同时,还提到了解决SSL证书验证错误的方法和Python生成图片的基本操作。
8080

被折叠的 条评论
为什么被折叠?



