python-error-notes

(1) command ‘C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe’ failed with exit status 4

  1. 把cl.exe所在目录添加到环境变量里 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin
  2. 安装VS 2015

visual-cpp-build-tools
vs2015离线安装包
离线安装包-国内

安装大概需要80分钟左右

(2) UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb6 in position 30: invalid start byte

Installing collected packages: Twisted, Scrapy
  Running setup.py install for Twisted ... error
Exception:
Traceback (most recent call last):
  File "c:\professionsofware\python\python36\lib\site-packages\pip\compat\__init__.py", line 73, in console_to_str
    return s.decode(sys.__stdout__.encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 30: invalid start byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\professionsofware\python\python36\lib\site-packages\pip\basecommand.py", line 215, in main
    status = self.run(options, args)
  File "c:\professionsofware\python\python36\lib\site-packages\pip\commands\install.py", line 342, in run
    prefix=options.prefix_path,
  File "c:\professionsofware\python\python36\lib\site-packages\pip\req\req_set.py", line 784, in install
    **kwargs
  File "c:\professionsofware\python\python36\lib\site-packages\pip\req\req_install.py", line 878, in install
    spinner=spinner,
  File "c:\professionsofware\python\python36\lib\site-packages\pip\utils\__init__.py", line 676, in call_subprocess
    line = console_to_str(proc.stdout.readline())
  File "c:\professionsofware\python\python36\lib\site-packages\pip\compat\__init__.py", line 75, in console_to_str
    return s.decode('utf_8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 30: invalid start byte

编码问题
换个编码试试就可以了

(3) ModuleNotFoundError: No module named ‘win32api’

缺少模块,安装对应的模块就可以了

pip install pypiwin32

(4) UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\u2022’ in position 4030: illegal multibyte sequence

编码问题

在写文件时使用 open(filename, mode=’a’, encoding=’utf-8’) 强制使用UTF-8编码
在代码编码,输出,字符串全部使用UTF-8

open(filename, mode='a', encoding='utf-8')

(5) an integer is required when open()’ing a file as utf-8

输入的参数少了,或者加上key

open(filename, mode='rb', encoding=None, errors='strict', buffering=1)

(6) json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

问题原因:用单引号包装对应字段,按照规范应该是双引号包装字段

方法一: 把单引号替换成双引号
str.replace("'", "\""))

方法二:使用 demjson 包
pip install demjson json_obj = demjson(json_string)

Python解析json之ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

(7) RequestsDependencyWarning: urllib3 (1.24.3) or chardet (3.0.4) doesn’t match a supported version!

/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/weikeqin1/WorkSpaces/python/python-study/src/car/car_deal_new_data.py
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.24.3) or chardet (3.0.4) doesn't match a supported version!
  RequestsDependencyWarning)

解决办法
pip install --upgrade urllib3
pip install --upgrade requests

(8) TypeError: bad operand type for unary +: ‘str’

TypeError: bad operand type for unary +: 'str'
  1. 没有把一元运算符做字符串,加个括号解决
  2. 拼接字符串的时候多,导致语义发生变化 sql = (" insert into t1 (brand_id, brand_name) values ( " + str(brand_id) + ", ' " + brand_name, +"', 'M') ; \r\n") brand_name后多了一个,
  3. 换行时拼接错误导致,如下,+ \ 应该把 + 去掉
moto_sql = ( " insert into t ( brand_id, brand_name) values ( "  + \
           + str(brand_id) + ", '" + brand_name +"' ; "

python这个太头疼了

References

[1] python-pip-on-windows-command-cl-exe-failed
[2] WindowsCompilers
[3] importerror-no-module-named-win32api
[4] Python解析json之ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
[5] requests请求出现RequestsDependencyWarning异常