1. 从文件中读取数据
(1).读取整个文件
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents)
输出的实际内容相比于原始文件,该输出唯一不同的地方是末尾多了一个空行。
为何会多出这个空行呢?
因为read() 到达文件末尾时返回一个空字符串,而将这个空字符串显示出来时就是一个空行。
要删除多出来的空行,可在print 语句中使用rstrip() :
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents.rstrip())
(2).文件路径
当你将类似pi_digits.txt这样的简单文件名传递给函数open() 时,Python将在当前执行的文件(即.py程序文件)所在的目录中查找文件。
相对文件路:
with open('text_files/filename.txt') as file_object:
在Windows系统中,在文件路径中使用反斜杠(\ )而不是斜杠(/ ):
with open('text_files\filename.txt') as file_object:
绝对文件路径:
file_path = '/home/ehmatthes/other_files/text_files/filename.txt'
with open(file_path) as file_object:
(3).逐行读取
filename = 'pi_digits.txt'
with open(filename) as file_object:
for line in file_object:
print(line)
我们打印每一行时,发现空白行更多了.
为何会出现这些空白行呢?
因为在这个文件中,每行的末尾都有一个看不见的换行符,而print 语句也会加上一个换行符。
因此每行末尾都有两个换行符:一个来自文件,另一个来自print 语句。
要消除这些多余的空白行,可在print 语句中使用rstrip() :
print(line.rstrip())
(4).创建一个包含文件各行内容的列表
filename = 'pi_digits.txt'
with open(filename) as file_object:
lines = file_object.readlines()
for line in lines:
print(line.rstrip())
方法readlines() 从文件中读取每一行,并将其存储在一个列表中;
接下来,该列表被存储到变量lines 中;
在with 代码块外,我们依然可以使用这个变量。
(5).包含一百万位的大型文件
只打印到小数点后50位
print(pi_string[:52] + "...")
(6).文件中包含字符串
birthday = input("Enter your birthday, in the form mmddyy: ")
if birthday in pi_string:
print("Your birthday appears in the first million digits of pi!")
else:
print("Your birthday does not appear in the first million digits of pi.")
2. 写入文件
(1).写入空文件
filename = 'programming.txt'
with open(filename, 'w') as file_object:
file_object.write("I love programming.")
(2).写入多行
filename = 'programming.txt'
with open(filename, 'w') as file_object:
file_object.write("I love programming.\n")
file_object.write("I love creating new games.\n")
(3).附加到文件
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 love creating apps that can run in a browser.\n")
3. 异常
(1). 处理ZeroDivisionError 异常
使用try-except 代码块
try:
answer = int(first_number) / int(second_number)
except ZeroDivisionError:
print("You can't divide by 0!")
else:
print(answer)
(2). 处理FileNotFoundError 异常
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)
(3). 分析文本
方法split() 以空格为分隔符将字符串分拆成多个部分,并将这些部分都存储到一个列表中。
>>> title = "Alice in Wonderland"
>>> title.split()
['Alice', 'in', 'Wonderland']
(4). 失败时一声不吭
except 代码块中明确地告诉Python什么都不要做,Python有一个pass 语句,可在代码块中使用它来让Python什么都不要做.
def count_words(filename):
"""计算一个文件大致包含多少个单词"""
try:
--snip--
except FileNotFoundError:
pass
else:
--snip--
filenames = ['alice.txt', 'siddhartha.txt', 'moby_dick.txt', 'little_women.txt']
for filename in filenames:
count_words(filename)
4. 存储数据
(1). 使用json.dump() 和json.load()
下面演示了如何使用json.dump() 来存储数字列表:
使用函数json.dump() 将数字列表存储到文件numbers.json
import json
numbers = [2, 3, 5, 7, 11, 13]
filename = 'numbers.json'
with open(filename, 'w') as f_obj:
json.dump(numbers, f_obj)
使用json.load() 将这个列表读取到内存中:
import json
filename = 'numbers.json'
with open(filename) as f_obj:
numbers = json.load(f_obj)
print(numbers)
(2). 保存和读取用户生成的数据
import 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 + "!")
现在再编写一个程序,向其名字被存储的用户发出问候:
import json
filename = 'username.json'
with open(filename) as f_obj:
username = json.load(f_obj)
print("Welcome back, " + username + "!")