python常见问题(1)

  1. python2转python3,在python/Tools/scipts文件夹下,运行python 2to3.py -w 文件夹路径
  2. 遇到write() argument must be str, not bytes问题,可以尝试加上”wb”参数,即 open('xx.pkl','wb)
  3. 遇到'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte,可以尝试加上”rb”参数,即open('xx','rb')
  4. python中键映射多个值,原本写法:
    d = {}
    for key, value in pairs:
        if key not in d:
            d[key] = []
        d[key].append(value)

    快捷方法:

    import collections
    d = collections.defaultdict(list)
    for key, value in pairs:
        d[key].append(value)

《python常见问题(1)》有1个想法

发表评论

邮箱地址不会被公开。 必填项已用*标注