为了账号安全,请及时绑定邮箱和手机立即绑定

python3中字符编码的问题?

/ 猿问

python3中字符编码的问题?

qq_訫恋_0 2018-05-20 00:29:59

Traceback (most recent call last):

  File "restzhong.py", line 183, in <module>

    caidan()

  File "restzhong.py", line 43, in caidan

    selected()

  File "restzhong.py", line 129, in selected

    s1=f.read()

  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode

    return codecs.ascii_decode(input, self.errors)[0]

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)

这错误啥意思啊?mac下sublimetext报出来的


money=0
money2=0
class Lunch:
    def __init__(self):
        self.cus=Customer()
        self.emp=Employee()
    def order(self,foodName):
        self.cus.placeOrder(foodName,self.emp)
    def result(self):
        #ask the Customer what kind of food it has
        self.cus.printFood()
class Customer:
    def __init__(self):#initialize my food to none
        self.food=None
    def placeOrder(self,foodName,employee):#place order with an Employee
        self.food=employee.takeOrder(foodName)    
    def printFood(self): #print the name of my food
        print(self.food.name)
class Employee:
    def takeOrder(self,foodName) :#return a Food,wih requested name
        return Food(foodName)   
    
class Food:
    def __init__(self,name):#store food name
        self.name=name
if __name__=='__main__':
    def caidan():
        print("编号      菜      名      价格 ")
        print("1          肉末茄子         17  ")
        print("2          麻婆豆腐         9   ")
        print("3          青椒土豆丝       8   ")
        print("4          肉夹馍+凉皮      15  ")
        print("5          大盘鸡           30   ")
        print("6          蘑菇炒油菜       12  ")
        selected()
    
    
    
    def istrue(num):
        try:
            int(num) 
            return True
        except ValueError:
            return False  
    
    def dian(s,context):
            global money
            global money2
            f2=open('aaaaa.txt','w')
            f2.write(context)
            f3=open('money.txt','r')
            if s=='1':
                f2.write("肉末茄子*(1)  17元"+"\n")
                money1=f3.read()
                x=Lunch()
                x.order('肉末茄子')
                x.result()
                if istrue(money1):
                    money2=17+int(money1)
                else:
                    money2=17
            elif s=='2':
                f2.write("麻婆豆腐*(1)  9元"+"\n")
                money1=f3.read()
                y=Lunch()
                y.order('麻婆豆腐')
                y.result()
                if istrue(money1):
                    money2=9+int(money1)
                else:
                    money2=9       
            elif s=='3':
                f2.write("青椒土豆丝*(1)  8元"+"\n")
                money1=f3.read()
                z=Lunch()
                z.order('青椒土豆丝')
                z.result()
                if istrue(money1):
                    money2=8+int(money1)
                else:
                    money2=8  
            elif s=='4':
                f2.write("肉夹馍+凉皮*(1)  15元"+"\n")
                money1=f3.read()
                d=Lunch()
                d.order('肉夹馍+凉皮')
                d.result()
                if istrue(money1):            
                    money2=23+int(money1)
                else:
                    money2=23   
            elif s=='5':
                f2.write("大盘鸡*(1)  30元"+"\n")
                money1=f3.read()
                e=Lunch()
                e.order('大盘鸡')
                e.result()
                if istrue(money1):
                    money2=8+int(money1)
                else:
                    money2=8      
            elif s=='6':
                f2.write("蘑菇炒油菜*(1)  12元"+"\n")
                money1=f3.read()
                f=Lunch()
                f.order('蘑菇炒油菜')
                f.result()
                if istrue(money1):
                    money2=12+int(money1)
                else:
                    money2=12      
                
            f3=open('money.txt','w')
            f2=open('aaaaa.txt','w')
            f2.write(context)
            f3.write(str(money2))
            f2.close()
            f3.close()
    def selected():
        f=open("aaaaa.txt","r")
        s1=f.read().decode()
        f.close()
        print("                     ")
        print("已点菜品:"+"\n"+s1)
        f3=open('money.txt','r')
        moneynum=f3.read()
        f3.close()
        print("小计:"+moneynum)
        print("                        ")
        selete=input("请选择您喜爱的菜品")
        dian(selete,s1)
        one=input("确认订单请按y,退出请按n,按任意键继续点单")
        if one=='y':
                    f=open("aaaaa.txt","r")
                    s1=f.read()
                    f.close()
                    print("                     ")
                    print("所有菜品:"+"\n"+s1)
                    f3=open('money.txt','r')
                    moneynum=f3.read()
                    f3.close()
                    print("小计:"+moneynum)
                    if istrue(moneynum):
                        print("此次订单需支付:"+str(moneynum))
                        f3=open('money.txt','w')
                        f3.write("")
                        f2=open('aaaaa.txt','w')
                        f2.write("")
                        f2.close()
                        f3.close()
                        print("订单完成,祝您用餐愉快")
                        
                    else :
                        print("您尚未选择商品")
                
        elif one=='n':
                    print("退出成功!")
                    f3=open('money.txt','w')
                    f3.write("")
                    f2=open('aaaaa.txt','w')
                    f2.write("")
                    f2.close()
                    f3.close()
                
        else :
                    selected()
                    
    print("<<<<<<<<<<欢迎来到外卖点餐系统>>>>>>>>>")
    print("1.查看菜单")
    print("2.退出")
    selectone=input("请输入您的选择")
    print("                        ")
    if selectone =='1':
        caidan()
    else :
        print("成功退出")

        



查看完整描述

3 回答

?
qq_晓歌_0

可能是money.txt文件编码问题,encode,decode试试

查看完整回答
反对 回复 2018-08-08
?
慧有未来

BUZHIDAO

查看完整回答
反对 回复 2018-05-20
?
Glitterior

把代码发上来啊

查看完整回答
反对 回复 2018-05-20

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信