码迷,mamicode.com
首页 > 其他好文 > 详细

实验四

时间:2021-05-24 14:12:19      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:spl   list   nes   while   ftime   for   code   pac   输入   

with open(‘data1_1.txt‘, ‘r‘, encoding = ‘utf-8‘) as f:
data=f.readlines()
n = 0
for line in data:
if line.strip(‘\n‘) == ‘‘:
continue
n+=1
print(f‘共{n}行‘)


with open(‘data1_1.txt‘, ‘r‘, encoding = ‘utf-8‘) as f:
n = 0
for line in f:
if line.strip(‘\n‘) == ‘‘:
continue
n += 1
print(f‘共{n}行‘)


with open(‘data1_2.txt‘, ‘r‘, encoding = ‘utf-8‘) as f:
n = 0
for line in f:
if line.strip() == ‘‘:
continue
n += 1
print(f‘共{n}行‘)


with open(‘data1_2.txt‘, ‘r‘, encoding = ‘utf-8‘) as f:
n = 0
for line in f:
if line.isspace():
continue
n += 1
print(f‘共{n}行‘)

技术图片

with open(‘data2.txt‘, ‘r‘, encoding = ‘utf-8‘) as f:
data = f.read().split(‘\n‘)
unique_line = []
for line in data:
if data.count(line) == 1:
unique_line.append(line)
print(f‘共{ len(unique_line) }独特行‘)
for i in unique_line:
print(i)技术图片技术图片

task6

with open("data6_1.txt","r",encoding="utf-8") as f:
    data=f.read().split("\n")

data1=[str(m).split("\t") for m in data]

data1.sort(key=lambda x: x[2],reverse=True)
with open("data6_2.txt","w+",encoding="utf-8") as f:
    for line in data1:
        for m in line:
            f.write(m+"\t")
            print(m+"\t",end="")
        f.write("\n")
        print()技术图片技术图片

 

import random
i=0
list=[]
a=int(input(‘输入随机抽点人数:‘))
with open(‘lucky.txt‘, ‘w‘) as f:
f.close()
with open(‘data7.txt‘, ‘r‘) as f:
data=f.readlines()
while i<a:
x=random.randint(1,80)
b=data[x]
print(b)
list.append(b)
i+=1
with open(‘lucky.txt‘, ‘w‘) as f:
for i in list:
f.write(i)技术图片技术图片

import random
import datetime
t=datetime.datetime.now()
datetime=t.strftime(‘%Y%m%d‘)
i=0
list=[]
a=int(input(‘输入随机抽点人数:‘))
with open(datetime+‘.txt‘, ‘w‘) as f:
f.close()
with open(‘data7.txt‘, ‘r‘) as f:
data=f.readlines()
while i<a:
x=random.randint(1,80)
b=data[x]
print(b)
list.append(b)
i+=1
with open(datetime+‘.txt‘, ‘w‘) as f:
for i in list:
f.write(i)

 技术图片技术图片

实验四

标签:spl   list   nes   while   ftime   for   code   pac   输入   

原文地址:https://www.cnblogs.com/czp666/p/14779074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!