共计 2763 个字符,预计需要花费 7 分钟才能阅读完成。
学习了对于文件解决以及代码测试的内容
以下所用文件能够在“图灵社区”收费下载
文件解决局部
# 建设相对路径的办法如下:# file_path = 'C:\Users\other_files\text_files\filename.txt'
# with open(file_path) as file_object
with open('text_file\pi_digits.txt') as file_object: # 传递绝对路径
contents = file_object.read()
print(contents)
# 文末会有 1 个空行,因为 read()达到文件开端时返回一个空字符串
print(contents.rstrip())
# 逐行读取
filename = 'text_file\pi_digits.txt'
with open(filename) as file_object:
for line in file_object:
print(line)
with open(filename) as file_object:
lines = file_object.readlines() # 读取每一行并存在一个列表中
for line in lines:
print(line.rstrip())
pi_string = ''
for line in lines:
pi_string += line.rstrip()
print(pi_string) # 右边也有空格
print(len(pi_string))
pi_string = ''
for line in lines:
pi_string += line.strip()
print(pi_string)
print(len(pi_string))
输入后果为:
3.1415926535
8979323846
2643383279
3.1415926535
8979323846
2643383279
3.1415926535
8979323846
2643383279
3.1415926535
8979323846
2643383279
3.1415926535 8979323846 2643383279
36
3.141592653589793238462643383279
32
检测生日是否在圆周率前百万位
这部分有须要输出的内容,故不展现输入
filename = 'text_file\pi_million_digits.txt'
with open(filename) as file_object:
lines = file_object.readlines() # 读取每一行并存在一个列表中
pi_string = ''
for line in lines:
pi_string += line.strip()
print(pi_string[:52] + "...")
print(len(pi_string))
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("Failed.")
文件写入及读取局部
# 写入空文件
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")
# 每次输出都会笼罩本来的内容
# 以追加的形式关上
with open(filename,'a') as file_object:
file_object.write("I love finding meaning in large database.\n")
file_object.write("I love creating apps that run in a browser.\n")
# 应用 try-except 代码块解决异样
try:
print(5/0)
except ZeroDivisionError:
print("You can't divide by zero!")
# 并非真正的解决,而是以不报错的形式持续运行
while True:
first_num = input("\nFirst number:")
if first_num == 'q':
break
second_num = input("\nSecond number:")
try:
answer = int(first_num)/int(second_num)
except ZeroDivisionError:
print("You can't divide by zero!")
else:
print(answer)
# 失败时一声不吭:应用 pass 命令
# 应用 json 模块存储数据
import json
numbers = [2,3,5,7,11,13]
filename = 'number.json'
with open(filename,'w') as f_obj:
json.dump(numbers,f_obj)
with open(filename) as f_obj:
new_numbers = json.load(f_obj)
print(new_numbers)
函数测试局部
def get_formatted_name(first, last, middle=''):
if middle:
full_name = first + '' + middle +' ' +last
else:
full_name = first + ' ' +last
return full_name.title()
class NamesCase(unittest.TestCase):
def test_first_last_name(self):
formatted_name = get_formatted_name('janis', 'joplin')
self.assertEqual(formatted_name, 'Janis Joplin')
def test_first_middle_last_name(self):
formatted_name = get_formatted_name('janis', 'joplin', 'stupid')
self.assertEqual(formatted_name, 'Janis Stupid Joplin')
# 各种断言形式
# assertEqual
# assertNotEqual
# assertTrue: 判断是否为真
# assertFalse
# assertIn: 判断是否在列表中
# assertNotIn
# 办法 setup()
# 用以创立一个考察对象,一个答案列表
正文完