Python学习8---eval与字符串的转换、with语句

#Author:Du Yang 
#Data:2018/7/12
#字典转换字符串
location = {"辽宁":{"阜新":{"细河区":{"辽工大北校区":{}}}}}
print("原字典格式:%s \t"%type(location), location)
print("字典转字符串:%s\t"%type(str(location)),str(location))
print("字符串转字典:%s\t" % type(eval(str(location))),eval(str(location)))
###########################总结############################
#str()可将列表,字典,元组转化为字符串
#而eval()则是用于将经过str()转化的字符串,还原为其原本对应的类型(如:字典,元组,列表);

----------------------------------------------------------------------------------------------------------------------------------

with open('春晓','r',encoding='utf-8') \as file_read, open('春晓1',"w",encoding='utf-8') as file_write:
    {...对应操作...}

使用with语句,不需要关注close()函数,系统会自动关闭保存

猜你喜欢

转载自blog.csdn.net/qq_33661910/article/details/81018827