- 学习-Python文件之上下文管理语句with
filename = input()
with open('src/step1/file/'+filename) as f:
print(f.readlines())
- 练习-Python文件之上下文管理语句with
path = input()
start = int(input())
end = int(input())
with open(path) as f:
f.seek(start)
print(f.read(end-start))
- 学习-Python文件之文本文件的随机读写
path = input()
f = open(path,'rb')
f.seek(-4,2)
text = f.read()
print('文件中的数字内容为:'+text.decode())
- 练习-Python文件之文本文件的随机读写
start = int(input()) # 读取内容的开始位置
end = int(input()) # 读取内容的结束位置
# 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码
########## Begin ##########
# 读取文件中指定位置的内容
test = open("src/step1/test.txt","r")
test.seek(start)
n = end - start
print(test.read(n))
test.close()
########## End ##########
- 学习-Python文件之文本文件的顺序读写
import re
f = open('src/step1/data.csv')
content = f.read().encode()
r = re.search(b'\d+[.]?\d+',content)
print('文件中的数字内容为:',r[0].decode(),sep='')
- 练习-Python文件之文本文件的顺序读写
with open(input()) as f:
print(f.read())
- 学习-Python文件之文本文件的读取
f = open("src/step1/data.txt",'w')
print('文件打开方式为:',f.mode,sep='')
- 练习-Python文件之文本文件的读取
path = input()
f = open(path,'r')
# 第二步:打印文件名
print('文件名为:%s' % f.name)
# 第三步:关闭文件
f.close()
########## End ##########
if f.closed:
print("文件已关闭")
else:
print("文件未关闭")
- Python 计算思维训练——文件操作与异常处理
9.1从文件中读取数据:信息时代已经到来
#coding=utf-8
#输入n
n = int(input())
# 请在此添加代码,实现编程要求
#********** Begin *********#
with open('src/Step1/test.txt') as f:
for i in range(n):
print(f.readline().strip())
#********** End **********#
9.2将信息写入文件:会读会写方为正道
# 请在此添加代码,将字符串s输入到test2.txt中
#********** Begin *********#
with open('src/Step2/test2.txt','w') as f:
f.write(s)
#********** End **********#
#输出test2.txt中的内容
with open('src/Step2/test2.txt') as file_object:
lines = file_object.readlines()
for line in lines:
print(line.rstrip())
9.3异常处理:敢于在错误面前吭声
#coding=utf-8
import math
#输入整数a
a = int(input())
try:
answer = math.sqrt(a)
# 请在此添加代码,实现编程要求
#********** Begin *********#
print('%.1f' % answer)
except:
print("We can't take a root by minus")
- Python 计算思维训练——输入和错误处理
10.1交互式输入 - 输出前 n 个偶数
#coding=utf-8
# 请在此处填写代码
#********** Begin **********#
for i in range(2,int(input())*2+1,2):
print(i)
10.2可执行对象内置函数 - 计算公式的微分求导
#coding=utf-8
from math import *
formula = input()
# 请在此处填写代码
#********** Begin **********#
h = 1E-5
x = float(input())
f1 = eval(formula.replace('x','(x+h)'))
f2 = eval(formula.replace('x','(x-h)'))
print('%.2f' % float((f1-f2)/(2*h)))
#********** End **********#
10.3文件读写 - 将二维表的内容写入文件
#coding=utf-8
def write_file():
# 请在此处填写代码
#********** Begin **********#
data = [[ 0.75, 0.29619813, -0.29619813, -0.75 ],
[ 0.29613, 0.116978, -0.117778, -0.29613],
[-0.29, -0.11698, 0.11697778, 0.29619813],
[-0.75, -0.29619813, 0.29619813, 0.75 ]]
f = open('src/step3/output/data.txt','w')
for l in data:
for i in l:
s = '%14.8f' % i
f.write(s)
f.write('\n')
10.4库函数 - 利率计算库
#coding=utf-8
# 请在此处填写代码
#********** Begin **********#
from math import *
def present_amount(A0,p,n):
return A0*(1+p/(360*100))**n
def initial_amount(A,p,n):
return A*(1+p/(360*100))**(-n)
def days(A0,A,p):
return log(A/A0)/log(1+p/(360*100))
def annual_rate(A0,A,n):
return 360*100*((A/A0)**(1/n)-1)
- 用户登录
import os
# 判断是否首次使用系统
def is_first_start():
if os.path.exists('flag.txt') == False:
flag = open('flag.txt', 'w+')
flag.write('1')
flag.close() # 关闭文件
init() # 初始化资源
print_login_menu() # 打印登录菜单
user_select() # 选择用户
else:
flag = open('flag.txt', 'r')
word = flag.read()
if len(word) == 1:
init() # 初始化资源
print_login_menu() # 打印登录菜单
user_select() # 选择用户
# 初始化管理员
def init():
if os.path.exists('users') == False:
#********** Begin **********
# 创建并打开管理员账户文件
root_file = open('u_root.txt','w')
# 写入管理员信息
root_file.write('root 123456')
# 关闭管理员账户文件
root_file.close()
# 创建普通用户文件夹
os.mkdir('users')
#********** End **********
# 打印登录菜单
def print_login_menu():
print('----用户登录----')
print('1-管理员登陆')
print('2-普通用户登陆')
print('--------------')
# 用户选择
def user_select():
while True:
user_type_select = input('请选择用户类型:\n')
if user_type_select == '1': # 管理员登陆验证
root_login()
break
elif user_type_select == '2': # 普通用户
while True:
select = input('是否需要注册?(y/n):\n')
#********** Begin **********
print('----用户注册----')
if select == 'y':
# 用户注册
user_register()
user_login()
break
# 用户登录
else:
user_login()
break
break
#********** End **********
else:
print('输入有误,请重新选择')
# 管理员登陆
def root_login():
while True:
print('****管理员登陆****')
root_number = input('请输入账户名:\n')
root_password = input('请输入密码:\n')
# file_root = open('u_root.txt', 'r')
# root = eval(file_root.read())
#********** Begin **********
# 只读打开文件
file_root = open('u_root.txt', 'r')
# 读取账户信息
root = file_root.read().split()
# 信息匹配
if root[0] == root_number and root[1] == root_password:
print('登陆成功!')
break
#********** End **********
# 用户注册
def user_register():
user_id = input('请输入账户名:\n')
user_pwd = input('请输入密码:\n')
user_name = input('请输入昵称:\n')
#********** Begin **********
# 创建用户文件
user = open('users/'+user_id,'w')
# 写入
user.write(user_pwd+' '+user_name)
# 保存关闭
user.close()
#********** End **********
# 普通用户登录
def user_login():
while True:
print('****普通用户登录****')
user_id = input('请输入账户名:\n')
user_pwd = input('请输入密码:\n')
#********** Begin **********
# 读取文件
if os.path.exists('users/'+user_id) == False:
return '用户不存在'
user = open('users/'+user_id)
content = user.read().split()
# 比对账号,密码
if content[0] == user_pwd:
print('登录中····')
print('登录成功!')
break
else:
print('登陆失败')
break
#********** End **********
is_first_start()
- 通讯录
import sys
import json
class TelephoneBook:
# 展示功能菜单
def show_menu(self):
print("*" * 20)
print("欢迎使用[通讯录] V1.0")
print("1. 新建联系人")
print("2. 查询联系人")
print("0. 退出系统")
print("*" * 20)
# 新建联系人的功能
def add_info(self):
name_str = input("请输入姓名:\n")
phone_num = input("请输入电话:\n")
qq_num = input("请输入QQ号码:\n")
mail_adr = input("请输入邮箱:\n")
#********** Begin **********
# 将数据封装到字典中
dic = {'姓名':name_str, '手机号':phone_num, 'qq':qq_num, 'mail':mail_adr}
# 将字典转换为str,然后再使用write()写入到通讯录的文本文件中
with open('通讯录.txt','w', encoding='utf-8') as f:
f.write(str(dic))
print(f'成功添加{name_str}为联系人')
#********** End **********
# 显示联系人信息
def show_info(self):
file = open("通讯录.txt", mode='r', encoding='utf-8')
# 如果通讯录.txt文件不为空时,执行下面代码
if len(file.read()) != 0:
#********** Begin **********
# 读取通讯录.txt文件中的内容
file.seek(0)
dic = eval(file.read())
name = input('请输入要查询的姓名:\n')
if dic['姓名'] == name:
for k,v in dic.items():
print(k,v,sep=':')
else:
print('联系人不存在')
#********** End **********
else:
print("通讯录为空")
def main(self):
while True:
self.show_menu()
# 判断用户输入的功能指令
action_str = input("请选择操作功能:\n")
if action_str.isdigit() is True:
if int(action_str) == 1:
self.add_info()
elif int(action_str) == 2:
self.show_info()
elif int(action_str) == 0:
sys.exit()
else:
print('请输入正确的指令')
# 创建一个TelephoneBook类对象,调用main()方法
if __name__ == '__main__':
tb = TelephoneBook()
tb.main()
- 身份证归属地查询
import json
#********** Begin **********
# 打开并读取文件
f = open('./身份证码值对照表.txt')
content = f.read()
content_dict = json.loads(content) # 转换为字典类型
address = input('请输入身份证前6位:')
print(address)
for key, val in content_dict.items():
if key == address:
print(val)
# 关闭文件
#********** End **********