python 奇怪的知识


向量梯度

  • vector = [a, b, c, d, e, f]
  • 边界元素: a的梯度= (b-a)/1; f的梯度=(f-e)/1
  • 中间元素: b的梯度= (c-a)/2; 其他中间元素类似
  • 例子:
vector = [3, 4, 7, 8, 1, 6, 5]
np.gradient(vector)
Out[4]: array([ 1.,  2.,  2., -3., -1.,  2., -1.])

pip 报错 ‘UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x98 in position 1130: illegal multibyte sequence’

  • 报错如下:
...

File "C:\Users\x407\AppData\Local\Programs\Python\Python38-32\lib\configparser.py", line 697, in read
    self._read(fp, filename)
  File "C:\Users\x407\AppData\Local\Programs\Python\Python38-32\lib\configparser.py", line 1017, in _read
    for lineno, line in enumerate(fp, start=1):
UnicodeDecodeError: 'gbk' codec can't decode byte 0x98 in position 1130: illegal multibyte sequence

解决

  • 在文件 C:\Users\x407\AppData\Local\Programs\Python\Python38-32\lib\configparser.py 679 行将代码改成:
def read(self, filenames,  encoding='utf-8'): # encoding=None, 这就是679 行
    """Read and parse a filename or an iterable of filenames.

    Files that cannot be opened are silently ignored; this is
    designed so that you can specify an iterable of potential
    configuration file locations (e.g. current directory, user's
    home directory, systemwide directory), and all existing
    configuration files in the iterable will be read.  A single
    filename may also be given.

    Return list of successfully read files.
    """
    if isinstance(filenames, (str, bytes, os.PathLike)):
        filenames = [filenames]
    read_ok = []
    for filename in filenames:
        try:
            with open(filename, encoding=encoding) as fp:
                self._read(fp, filename)
        except OSError:
            continue
        if isinstance(filename, os.PathLike):
            filename = os.fspath(filename)
        read_ok.append(filename)
    return read_ok

  • 就是编码指定成 utf-8;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值