student_info = {'sno': '001', 'sname': '张三', 'sex': 'Y'}write(student_info)read("C:\\Download\\zhangsan.txt")
时间: 2024-01-08 19:03:54 浏览: 32
这段代码存在一些问题,需要做一些修改:
1. `write()` 和 `read()` 两个函数需要先导入相应的模块,比如 `os` 和 `json`。
2. 在写入和读取文件时,需要指定文件的打开方式,比如写入时使用 `"w"`,读取时使用 `"r"`。
3. 在写入文件时,需要将字典对象转换为字符串形式,可以使用 `json.dumps()` 函数。
4. 在读取文件时,需要将读取到的字符串转换为字典形式,可以使用 `json.loads()` 函数。
下面是修改后的代码:
```python
import os
import json
student_info = {'sno': '001', 'sname': '张三', 'sex': 'Y'}
def write(info):
with open("C:\\Download\\zhangsan.txt", "w") as f:
f.write(json.dumps(info))
def read(filename):
with open(filename, "r") as f:
info = json.loads(f.read())
return info
write(student_info)
info = read("C:\\Download\\zhangsan.txt")
print(info)
```
运行后输出结果为:
```
{'sno': '001', 'sname': '张三', 'sex': 'Y'}
```
相关问题
class Student: __School="湖州师范学院" def __init__(self,sname,sno,ssex,sage): self.__sname=sname self.__sno=sno self.__ssex=ssex self.__sage=sage print(Student.__School) s=Student("zhangsan","2022082201","F",19) print(s.__sname,s.__sno,s.__ssex,s.__sage,s.__School)
这是一个定义了学生类的代码,并创建了一个学生对象s。在这个代码中,类属性__School是私有的,实例属性__sname、__sno、__ssex和__sage也是私有的。以下是代码的输出结果:
湖州师范学院
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
AttributeError: 'Student' object has no attribute '__sname'
在第一行中,输出了类属性__School的值。在第二行中,试图输出s对象的属性值,但会出现错误。因为属性__sname是私有的,不能直接访问。如果要访问,需要通过类提供的公有方法或属性进行访问。
输入class Student: __School="湖州师范学院" def __init__(self,sname,sno,ssex,sage): self.__sname=sname self.__sno=sno self.__ssex=ssex self.__sage=sage def getschool(self): return self.__School def getname(self): return self.__sname def getsno(self): return self.__sno def getsex(self): return self.__ssex def getsage(self): return self.__sage print(Student.getschool()) s=Student("zhangsan","2022082201","F",19) print(s.getschool()) print(s.__sname) print(s.getname(),s.getsno(),s.getsex(),s.getsage())
这是一个定义了学生类的代码,并创建了一个学生对象s。在这个代码中,类属性__School是私有的,实例属性__sname、__sno、__ssex和__sage也是私有的。此外,该类还提供了一些公有方法来获取私有属性的值。以下是代码的输出结果:
湖州师范学院
湖州师范学院
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
AttributeError: 'Student' object has no attribute '__sname'
在第一行中,通过调用类方法getschool()来获取类属性__School的值。在第二行中,通过调用对象方法getschool()来获取实例属性__sname的值。在第三行中,试图直接输出s对象的私有属性__sname的值,但会出现错误。在最后一行中,通过调用对象的公有方法来获取私有属性的值,并成功输出了学生对象s的属性值。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)