Python问题汇总

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,utf8,ssl)