with open('data6.csv', 'r',encoding = 'gbk') as f: data = f.readlines() processed_data = [] for i in range(1, len(data)): row = data[i].strip() processed_item = round(float(row)) processed_data.append(processed_item) with open('data6_processed.csv', 'w',encoding = 'gbk') as f: f.write('原始数据,四舍五入后的数据\n') for i in range(len(processed_data)): f.write(data[i+1].strip() + ',' + str(processed_data[i])+'\n') print('原始数据:') print( [float(d.strip()) for d in data[1:]]) print('四舍五入后的数据:') print(processed_data)
with open('data7.csv', 'r',encoding='gbk') as f: data = f.readlines() datas = {} for i in range(1, len(data)): row = data[i].strip().split(',') student_id, name, major, score = row[0], row[1], row[2], int(row[3]) if major not in datas: datas[major] = [] datas[major].append({'学号': student_id, '姓名': name, '分数': score}) with open('data7_processed.csv', 'w',encoding='gbk') as f: f.write('学号,姓名,专业,分数\n') for major in sorted(datas.keys()): major_data = sorted(datas[major], key=lambda k: k['分数'], reverse=True) for item in major_data: f.write(item['学号'] + ',' + item['姓名'] + ',' + major + ',' + str(item['分数']) + '\n') print('{:<8}\t{:<8}\t{:<8}\t{}'.format('学号', '姓名', '分数', '专业')) for major in sorted(datas.keys()): major_data = sorted(datas[major], key=lambda k: k['分数'], reverse=True) for item in major_data: print('{:<8}\t{:<8}\t{:<8}\t{}'.format(item['学号'], item['姓名'], str(item['分数']), major))
with open('hamlet.txt', 'r') as f: text = f.read() lines = text.split('\n') words = text.split() num_chars = len(text) num_spaces = text.count(' ') print('行数:', len(lines)) print('单词数:', len(words)) print('字符数:', len(text)) print('空格数:', num_spaces) with open('hamlet_with_line_number.txt', 'w') as f: for i in range(len(lines)): f.write('{:>4} {}\n'.format(i+1, lines[i]))
import datetime def is_valid(id_num): """ 判断身份证号码是否有效 """ if len(id_num) != 18: return False if not id_num[:17].isdigit(): return False if id_num[17] not in '0123456789X': return False return True with open('data9_id.txt', 'r',encoding='utf-8') as f: lines = f.readlines() id_list = [] for line in lines: line = line.strip() if line.startswith('姓名'): continue name, id_num = line.split(',') if is_valid(id_num): birth_str = id_num[6:14] birth_date = datetime.datetime.strptime(birth_str, '%Y%m%d') age = (datetime.datetime.now() - birth_date).days // 365 id_list.append((name, birth_date, age)) id_list.sort(key=lambda x: x[2], reverse=True) for name, birth_date, age in id_list: print('{}\t{}\t{}'.format(name, birth_date.strftime('%Y-%m-%d'), age))
import random import datetime with open('data10_stu.txt', 'r', encoding='utf-8') as f: data = f.readlines() n = int(input('随机抽点人数:')) ran_data = random.sample(data, n) for line in ran_data: line = line.strip().split('\t') print('{:<12}{:<12}{}'.format(line[0], line[1], line[2])) file_name = datetime.datetime.now().strftime('%Y%m%d') + '.txt' with open(file_name, 'w', encoding='utf-8') as f: f.write('学号\t姓名\t班级\n') f.writelines(ran_data)
import random import datetime with open('data10_stu.txt', 'r', encoding='utf-8') as f: data = f.readlines() print(f"{'抽点开始':*^40}") while True: n = int(input('输入随点抽取人数:')) if n == 0: break ran_data = random.sample(data, n) for line in ran_data: line = line.strip().split('\t') print('{:<12}{:<12}{}'.format(line[0], line[1], line[2])) print(f"{'抽点结束':*^40}") file_name = datetime.datetime.now().strftime('%Y%m%d') + '.txt' with open(file_name, 'a', encoding='utf-8') as f: f.writelines(set(ran_data))
标签:num,实验,processed,print,line,data,id From: https://www.cnblogs.com/yhyy/p/17458506.html