01 遇到的错误 遇到错误的源代码如下:(修改后)

import time
from  multiprocessing import Process
import json
from multiprocessing import Lock


def show():
    with open(r'E:\text1\day36\ticket','r',encoding='utf-8') as f:
        dit=json.load(f)
    print('%s余票数:\033[32m%s\033[0m' %(dit['place'],dit['余票']))

def buy(i,lock):                          # 必须传入一个锁的对象
    lock.acquire()
    time.sleep(0.5)
    with open(r'E:\text1\day36\ticket','r',encoding='utf-8') as f:
        dit = json.load(f)
    if dit['余票']>0:
        print('%s  \033[32m购票成功\033[0m' %i)
        dit['余票']-=1
    else:
        print('%s  \033[31m无余票\033[0m' %i)
    time.sleep(0.1)
    with open(r'E:\text1\day36\ticket','w',encoding='utf-8') as f:
        json.dump(dit,f)
    lock.release()

if __name__ =='__main__':

    for i in range(10):
        p=Process(target=show)
        p.start()
    lock=Lock()                          # 实例化一个锁的对象
    # lock.acquire()
    # lock.release()
    for i in range(10):
        p=Process(target=buy,args=(i,lock))
        p.start()

ticket-text文件` {"place": "\u54c8\u5c14\u6ee8-->\u5317\u4eac", "\u4f59\u7968": 0}``

错误1 UnicodeDecodeError: 'gbk' codec can't decode byte 0xa8 in position 18: illegal multibyte sequence 错误的原因: gbk不能去解码bytes类型的数据,非法多字节序列 解决方法: with open(path,mode='r',encoding='utf-8') as f: 现在感觉是在不完全弄明白decode,encode之前,不要随便的省略编码类型;

错误2 json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) 错误的原因: 期望双引号中包含的属性名称 (double quotes 双引号) 解决的方法: 一般情况是不会出现这样的情况的,因为通过json.dumps()向文件中写入str的类型,再通过json.loads()来将文件中str的类型转换成原格式,是不会存在问题的, 要注意的就是,json.dumps()写入的str字符串,字符串是用双引号的(" "),所有要是自己写一个文件通过json.loads()/json.dumps()操作,将字符串用双引号就好.