实验5 文件应用编程
实验任务1:统计文件行数(不统计空白行)
task1_1.py
'''
统计文件data1.txt行数(不包括空白行)
data1.txt中的空白行包括由空格、Tab键(\t)、换行(\n)构成的空白行
'''
with open('data1.txt', 'r', encoding = 'utf-8') as f:
data = f.readlines()
n = 0
for line in data:
if not line.strip() == '':
n += 1
print(f'data1.txt共{n}行')
task1_2.py
'''
统计文件data1.txt行数(不包括空白行)
data1.txt中的空白行包括由空格、Tab键(\t)、换行(\n)构成的空白行
'''
with open('data1.txt', 'r', encoding = 'utf-8') as f:
n = 0
for line in f:
if not line.strip() == '':
n += 1
print(f'data1.txt共{n}行')
task1_3.py
'''
统计文件data1.txt行数(不包括空白行)
data1.txt中的空白行包括由空格、Tab键(\t)、换行(\n)构成的空白行
'''
with open('data1.txt', 'r', encoding = 'utf-8') as f:
n = 0
for line in f:
if not line.isspace():
n += 1
print(f'data1.txt共{n}行')
实验任务2:统计文件独特行行数(在文件中之出现一次的行)
task2.py
'''
统计数据文件data2.txt中独特行的行数
并打印输出独特行
'''
with open('data2.txt', 'r', encoding = 'utf-8') as f:
data = f.read().split('\n')
unique_line_lst = []
for line in data:
if data.count(line) == 1:
unique_line_lst.append(line)
n = len(unique_line_lst)
print(f'data2.txt共{n}行独特行')
for line in unique_line_lst:
print(line)
实验任务3:csv格式文件读写
task3_1.py
'''
把城市、人口信息写入数据文件city1.csv
使用python内置的文件操作实现
'''
title = ['城市', '人口(万)']
info = [['南京', '850'],
['纽约', '2300'],
['东京', '3800'],
['巴黎', '1000']]
with open('city1.csv', 'w', encoding = 'gbk') as f:
f.write(','.join(title) + '\n') # 写入标题行
for item in info: # 分行写入info
f.write(','.join(item) + '\n')
task3_2.py
'''
从文件city1.csv读取城市、人口信息,打印输出到屏幕
使用python内置的文件读写操作实现
'''
with open('city1.csv', 'r', encoding = 'gbk') as f:
data = f.read()
print(data.rstrip('\n'))
task3_3.py
'''
从文件city1.csv读取城市、人口信息,将其保存到里列表对象,保存形式诸如:
info = [ ['城市', '人口(万)'],
['南京', '850'],
['纽约', '2300'],
['东京', '3800'],
['巴黎', '1000'] ]
在屏幕上打印输出列表对象
使用python内置的文件读写操作实现
'''
with open('city1.csv', 'r', encoding = 'gbk') as f:
data = f.readlines()
# 打印中间处理结果(供查看)
print('data: ')
print(data)
info = [line.strip('\n').split(',') for line in data]
print('info:')
print(info)
task3_4
'''
把城市、人口信息写入数据文件city1.csv
使用python标准模块csv实现
'''
import csv
title = ['城市', '人口(万)']
info = [['南京', '850'],
['纽约', '2300'],
['东京', '3800'],
['巴黎', '1000']]
with open('city2.csv', 'w', encoding = 'gbk', newline = '') as f:
f_writer = csv.writer(f) # 为文件对象f创建一个writer对象
f_writer.writerow(title) # 通过writer对象的方法writerow()写入一行(标题行)
f_writer.writerows(info) # 通过writer对象的方法writerows()写入多行
task3_5.py
'''
从文件city2.csv读取城市、人口信息,在屏幕上打印输出
使用python标准模块csv实现
'''
import csv
with open('city2.csv', 'r', encoding = 'gbk') as f:
f_reader = csv.reader(f) # 为文件对象f创建一个reader对象
for line in f_reader:
print(line)
实验任务4:列出当前目录下所有的python文件(.py结尾的文件)
task4.py
'''
列出当前目录下所有.py文件
'''
import os, sys
print(os.path.basename(os.getcwd()))
# 将当前路径下所有.py文件名保存到py_file_lst中
py_file_lst = [file for file in os.listdir() if file.endswith('.py')]
# 遍历输出
for number, file in enumerate(py_file_lst, 1):
print(f'{number:-3d}. {file}')
实验任务5:random模块、datetime模块
random模块
datetime模块
实验任务6:文件数据四舍五入处理
import csv
with open('data6.csv','r',encoding="gbk") as f:
f_reader=csv.reader(f)
lines=[line for line in f_reader][1:]
data=[]
rounded_data = []
for line in lines:
value=float(line[0])
data.append(value)
rounded_value=round(value)
rounded_data.append(rounded_value)
with open('data6_processed.csv','w',encoding='gbk',newline='') as f:
f_writer=csv.writer(f)
f_writer.writerow(['原始数据','四舍五入后数据'])
for i in range(len(data)):
f_writer.writerow([data[i],rounded_data[i]])
print(f'原始数据:\n{data}')
print(f'四舍五入后数据:\n{rounded_data}')
实验任务7:成绩文件处理
import csv
with open('data7.csv','r',encoding='gbk') as f:
f_reader=csv.reader(f)
lines = [line for line in f_reader]
title=lines[0]
data=lines[1:]
data=sorted(data,key=lambda x : (x[2],-float(x[3])))
with open('data7_processed.csv','w',encoding='gbk') as f:
f_writer=csv.writer(f)
f_writer.writerow(title)
f_writer.writerows(data)
print(f'{"学号":{chr(12288)}<10s}{"姓名":{chr(12288)}<10s}{"专业":<10s}{"分数":<10s}')
for i in range(len(data)):
print(f'{data[i][0]:{chr(12288)}<10s}{data[i][1]:{chr(12288)}<10s}{data[i][2]:<10s}{data[i][3]:<10s}',)
实验任务8:文档统计处理
with open('hamlet.txt','r',encoding='gbk') as f:
data=[line.strip() for line in f.readlines()]
linecount=len(data)
wordcount=0
charcount=0
spacecount=0
for line in data:
wordcount+=len(line.split())
for j in line:
charcount+=1
if j.isspace():
spacecount+=1
print(f'行数:{linecount}')
print(f'单词数:{wordcount}')
print(f'字符数:{charcount}')
print(f'空格数:{spacecount}')
with open('hamlet_with_line_number.txt','w',encoding='gbk') as f:
for i in range(linecount):
f.write(str(i+1)+' '+data[i]+'\n')
实验任务9:身份信息批量处理
import datetime as dt
# 检查身份证号码是否有效
def is_valid(id):
if len(id) == 18:
if id[-1] == 'X':
id = id[0:-1]
if id.isdigit():
return True
else:
return False
elif id.isdigit():
return True
else:
return False
else:
return False
# 计算年龄
def calculate_age(year, month, day):
current_date = dt.datetime.now()
age = current_date.year - year
if (current_date.month, current_date.day) < (month, day):
age -= 1
return age
# 读取文件并解析数据
with open('data9_id.txt', 'r', encoding="utf-8") as f:
data_lines = [line.strip().split(',') for line in f.readlines()]
# 提取身份证号码和姓名
id_numbers = [line[1] for line in data_lines[1:]]
name_list = [line[0] for line in data_lines[1:]]
# 过滤有效姓名列表
name_processed = [name_list[i] for i in range(len(name_list)) if is_valid(id_numbers[i])]
# 提取出生日期信息
birth_list = [[id_numbers[i][6:10]] + [id_numbers[i][10:12]] + [id_numbers[i][12:14]] for i in range(len(id_numbers)) if is_valid(id_numbers[i])]
# 计算年龄列表
age_list = [calculate_age(int(sublist[0]), int(sublist[1]), int(sublist[2])) for sublist in birth_list]
# 格式化出生日期
birth_list_formatted = ['-'.join(sublist) for sublist in birth_list]
# 构建有效数据列表
valid_data = [[name_processed[i]] + [birth_list_formatted[i]] + [age_list[i]] for i in range(len(birth_list))]
# 按年龄降序排序
valid_data_sorted = sorted(valid_data, key=lambda x: -x[2])
# 打印表头
print(f'{"姓名"},{"出生日期"},{"年龄"}')
# 打印有效数据
for i in range(len(age_list)):
print(f'{valid_data_sorted[i][0]},{valid_data_sorted[i][1]},{valid_data_sorted[i][2]}')
实验任务10:随机抽点
task10_1.py
import datetime as dt
import random
def choose(n):
return random.sample(data,n)
with open('data10_stu.txt','r',encoding="utf-8") as f:
data=[line.strip() for line in f.readlines()]
stu_count=len(data)
n=int(input(f'输入随机抽点人数:'))
data_chosen=choose(n)
for i in range(n):
print(data_chosen[i])
task10_2.py
import datetime as dt
import random
def choose(n):
return random.sample(data,n)
with open('data10_stu.txt','r',encoding="utf-8") as f:
data=[line.strip() for line in f.readlines()]
stu_count=len(data)
endlist=[]
print(f'{"抽点开始":*^40}')
while True:
n=int(input(f'输入随机抽点人数:'))
if n!=0:
data_chosen=choose(n)
endlist.append(data_chosen)
for i in range(n):
print(data_chosen[i])
elif n==0:
break
print(f'{"抽点结束":*^40}')
date=str(dt.datetime.now().date())
with open(date,'w',encoding="utf-8") as f:
for i in range(len(endlist)):
for stu in endlist[i]:
f.write(stu)
f.write('\n')