实验五

with open('data6.csv','r',encoding='gbk')as f:
     data=list(f.readlines())
  data=[i.strip('\n') for i in data ]
  
  import decimal
  decimal.getcontext().rounding='ROUND_HALF_UP'
  title=[data[0],'四舍五入后的数据']
  data_new=[]
 data_o=[]
 for i in data:
     if not i.isalpha() is True:
         data_o.append(eval(i))
 print('原始数据:')
 print(data_o)
 for i in data:
    if not i.isalpha() is True:
         data_new.append([i])

 data_=[]
 for i in data:
     if not i.isalpha() is True:
         i=eval(i)
         if i-int(i)>=0.5:
             i=int(i)+1
            data_new[index].append(str(i))
            data_.append(i)
         else:
             i=int(i)
             data_new[index].append(str(i))
             data_.append(i)
 print('四舍五入后的数据:')
 print(data_)
 import csv
 with open('data6_processed.csv','w',encoding='gbk',newline='') as f :
     f_writer=csv.writer(f)
     f_writer.writerow(title)
     f_writer.writerows(data_new)

 

 任务7

with open('data7.csv','r',encoding = 'gbk') as f:
    data = f.readlines()
data_t = ['学号','姓名','专业','分数']
data1 = [i.strip('\n').split(',') for i in data]
data1.remove(data1[0])

data2=sorted(data1,key=lambda x:(x[2],-int(x[3])))
with open('data7_processed.csv','w',encoding = 'gbk') as f:
    f.write(','.join(data_t) + '\n')
    for item in data2:
        f.write(','.join(item) + '\n')

print(f'{"学号":10s}{"姓名":10s}{"专业":10s}{"分数":10s}')
for a,b,c,d in data2:
    print('{:<10s}{:<10s}{:<10s}{:<10}'.format(a,b,c,d))

 

 任务8

 lines = 0
  words = 0
  sum1 = 0
  space = 0
  with open('hamlet.txt','r',encoding='utf-8') as f:
     for line in f:
          word = line.split()
          lines += 1
          words += len(word)
         sum1 += len(line)
         for i in line:
             if i == ' ':
                 space += 1
             else:
                 pass
 print('hamlet.txt粗滤统计:')
 print(f'行数:{lines}')
 print(f'单词数:{words}')
 print(f'字符数:{sum1}')
 print(f'空格数:{space}')
  
 with open('hamlet.txt','r',encoding = 'utf-8') as f:
     text = f.readlines()
  
 for i in range(len(text)):
     text[i] = str(i+1) + ' ' + text[i]
  
 with open('hamlet_with_line_number.txt','w',encoding = 'utf-8' as f:
     f.writelines(text)

 

 

 任务9

def is_valid(sfz):
    if len(sfz) != 18:
        return False
    elif (sfz[:-1].isnumeric() and sfz[-1] == 'X') or sfz.isnumeric():
        return True
    else:return False


with open('data9_id.txt','r',encoding='utf-8') as f:
    data = f.read().split('\n')
del data[0]
data2 = []
data3 =[]
print('姓名,出生日期,年龄')
for i in data:
    lst = i.split(',')
    data2.append(lst)
lst2 = []
for i in data2:
    if is_valid(i[1]) == True:
        name = i[0]
        btd = i[1][6:14]
        age = str(2023 - int(i[1][6:10]))
        lst2.append([name,btd,age])
lst2.sort(key=lambda x:x[2],reverse=True)
for i in lst2:
    print(i[0],end=',')
    print(f'{i[1][:4]}-{i[1][4:6]}-{i[1][6:8]}',end=',')
    print(i[2])

 任务10

with open('data10_stu.txt','r',encoding='utf-8') as f:
    data = f.readlines()
print('{:*^40}'.format('抽点开始'))
n = int(input('输入随机抽点人数:'))
import random
x = []
x2 = ''
counts = 0
while counts < n:
    new = random.randint(0, len(data) - 1)
    if new in x:
        new = random.randint(0, len(data) - 1)
    else:
        counts +=1
        x.append(new)
for i in range(n):
    print(data[x[i]])
    x2 += data[x[i]]
    sum1 += n
with open('20230602.txt','w',encoding='utf-8') as f:
    f.writelines(x2)

 

 

with open('data10_stu.txt','r',encoding='utf-8') as f:
    data = f.readlines()
print('{:*^40}'.format('抽点开始'))
n = int(input('输入随机抽点人数:'))
import random
x = []
sum1 = 0
x2 = ''
while n != 0:
    counts = 0
    while counts < n:
        new = random.randint(0, len(data) - 1)
        if new in x:
            new = random.randint(0, len(data) - 1)
        else:
            counts +=1
            x.append(new)
    for i in range(sum1,sum1+n):
        print(data[x[i]])
        x2 += data[x[i]]
    sum1 += n
    n = int(input('输入随机抽点人数:'))

with open('20230602.txt','w',encoding='utf-8') as f:
    f.writelines(x2)

print('{:*^40}'.format('抽点结束'))

 

 

posted @ 2023-06-05 22:15  王响  阅读(14)  评论(0编辑  收藏  举报