本章学习系统的json类
import json
import pip
if __name__ == '__main__':
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents)
#文件路径
with open('D:\Documents\Downloads\使用前说明.txt') as file_object:
contents = file_object.read()
print(contents)
#逐行读取
filename = 'pi_digits.txt'
with open(filename) as file_object:
for line in file_object:
print(line)
#创建一个包含文件各行内容的列表
filename = 'pi_digits.txt'
with open(filename) as file_object:
lines = file_object.readline()
for line in lines:
print(line.rstrip())
#使用文件的内容
#包含一百万位的大型文件
#写入文件
'''保存数据的最简单的方式之一是将其写入到文件中。 通过将输出写入文件, 即便关闭包含程序输出的终端窗口, 这些输出也依然存在: 你可以在程序结束运行后查看这些输出,
可与别人分享输出文件, 还可编写程序来将这些输出读取到内存中并进行处理'''
filename = 'programming.txt'
with open(filename, 'w') as file_object:
file_object.write("I love programming.and i love ios ")
#写入多行
filename = 'programming.txt'
with open(filename,'w') as file_object:
file_object.write("I love programming.")
file_object.write("I love creating new games.")
#附加到文件
'''如果你要给文件添加内容, 而不是覆盖原有的内容, 可以附加模式 打开文件。 你以附加模式打开文件时, Python不会在返回文件对象前清空文件, 而你写入到文件的行都将添加
到文件末尾。 如果指定的文件不存在, Python将为你创建一个空文件'''
filename = 'programming.txt'
with open(filename, 'a') as file_object:
file_object.write("I also love finding meaning in large datasets.\n")
file_object.write("I also love finding meaning in large datasets.\n")
#异常的处理
try:
print(5/0)
except ZeroDivisionError:
print("You can't divide by zero!")
#使用异常避免崩溃
print("Give me two numbers, and I'll divide them.")
print("Enter 'q' to quit.")
# while True:
# first_number = input("\nFirst number: ")
# if first_number == 'q':
# break
# second_number = input("Second number: ")
# if second_number == 'q':
# break
# answer = int(first_number) / int(second_number)
# print(answer)
#处理FileNotFoundError 异常
'''使用文件时, 一种常见的问题是找不到文件: 你要查找的文件可能在其他地方、 文件名可能不正确或者这个文件根本就不存在。 对于所有这些情形, 都可使用try-except 代码
块以直观的方式进行处理'''
filename = 'alice.txt'
try:
with open(filename) as f_obj:
contents = f_obj.read()
except FileNotFoundError:
msg = "Sorry, the file " + filename + " does not exist."
print(msg)
def count_words(filename):
"""计算一个文件大致包含多少个单词"""
try:
with open(filename) as f_obj:
contents = f_obj.read()
except FileNotFoundError:
msg = "Sorry, the file " + filename + " does not exist."
print(msg)
else:
# 计算文件大致包含多少个单词
words = contents.split()
num_words = len(words)
print("The file " + filename + " has about " + str(num_words) + " words.")
filename = 'programming.txt'
count_words(filename)
#存储数据
'''模块json 让你能够将简单的Python数据结构转储到文件中, 并在程序再次运行时加载该文件中的数据。 你还可以使用json 在Python程序之间分享数据。 更重要的是, JSON数据
格式并非Python专用的, 这让你能够将以JSON格式存储的数据与使用其他编程语言的人分享。 这是一种轻便格式, 很有用, 也易于学习'''
#使用json.dump() 和json.load()
numbers = [2, 3, 5, 7, 11, 13]
filename = 'numbers.json'
with open(filename, 'w') as f_obj:
json.dump(numbers, f_obj)
print(numbers)
filename = 'numbers.json'
with open(filename) as f_obj:
numbers = json.load(f_obj)
print(numbers)
#保存和读取用户生成的数据
'''对于用户生成的数据, 使用json 保存它们大有裨益, 因为如果不以某种方式进行存储, 等程序停止运行时用户的信息将丢失'''
username = input("What is your name? ")
filename = 'username.json'
# with open(filename, 'w') as f_obj:
# json.dump(username, f_obj)
# print("We'll remember you when you come back, " + username + "!")
with open(filename) as f_obj:
username = json.load(f_obj)
print("Welcome back, " + username + "!")
#重构
print(pip.pep425tags.get_supported())
pass