xingshong 2022-07-14 14:23 采纳率: 50%
浏览 49
已结题

有哪位能帮我看一下代码?

from tkinter import Tk,simpledialog,messagebox
def read_from_file():
    with open('国家、首都名.txt')as file:
        for line in file:
            line=line.rstrip('\n')
            country,city=line.rstrip('/')
            the_world[country]=city
def write_to_file(country_name,city_name):
    with open('国家、首都名.txt','a') as file:
        file.write('\n'+country_name+'/'+city_name)
print('咨询专家 - 世界首都')
root=Tk()
root.withdraw()
the_world={}
read_from_file()
while True:
    query_country=simpledialog.askstring('国家','输入国家名称:')
    if query_country in the_world:
        result=the_world[query_country]
        messagebox.showinfo('回答','查询'+query_country+'的首都是'+result+'!')
    else:
        new_city=simpledialog.askstring('教我','我不知道!'+query_country+'的首都是哪里'+'?')
        the_world[query_country]=new_city
        write_to_file(query_country,new_city)
root.mainloop()

运行代码结果:

咨询专家 - 世界首都
Traceback (most recent call last):
  File "D:\方嘉铭 编程\请教专家.py", line 15, in <module>
    read_from_file()
  File "D:\方嘉铭 编程\请教专家.py", line 4, in read_from_file
    for line in file:
UnicodeDecodeError: 'gbk' codec can't decode byte 0x8c in position 15: illegal multibyte sequence
  • 写回答

1条回答 默认 最新

  • 快乐小土狗 2022-07-14 14:26
    关注

    with open('国家、首都名.txt', encoding='utf8')as file

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 7月22日
  • 已采纳回答 7月14日
  • 修改了问题 7月14日
  • 修改了问题 7月14日
  • 展开全部

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用