# 实验5

with open('data1_1.txt', 'r', encoding = 'utf-8') as f:

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 data:
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:

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)

3

with open('data3.txt','r',encoding='utf-8') as f :
with open('data3_processed.txt', 'w', encoding='utf-8') as f1:
data.pop(0)
data1=[eval(i) for i in data]
data2=[]
for i in data1:
if i - int(i) >= 0.5:
data2.append(int(i) + 1)
else:
data2.append(int(i))
data3=[int(i)for i in data2]
f1.write(f'原始数据\t四舍五入后的数据\n')
for j in range(len(data)):
f1.write(f'{data1[j]}\t\t{data2[j]}\n')
print(f'原始数据:\n{data1}\n四舍五入后的数据:\n{data2}')

4

with open('data4.txt','r',encoding='utf-8')as f:
with open('data4_processed.txt','w',encoding='utf-8')as f1:
data1=[i.strip('\n').split('\t') for i in data]
x=data1.pop(0)
data2=[sorted(data1,key=lambda x: (x[2], -int(x[3])))]
print('\t\t'.join(x))
f1.write('\t\t'.join(x)+'\n')
for i in data2:
for j in i:
print('\t'.join(j))
f1.write('\t'.join(j)+'\n')

5

with open('data5.txt','r',encoding='utf-8')as f:
with open('data5_with_line.txt', 'w', encoding='utf-8') as f1:
b = len(a.splitlines())
print('行数:', b)
e = len(a.split())
print('单词数:', e)
c = 0
for c, number in enumerate(a):
c += 1
print('字符数:', c)
d = 0
for i in a:
if i == ' ':
d += 1
print('空格数:', d)
a=a.split('\n')
n='\n'.join(a)

posted @ 2022-05-17 15:54  青桔水溶C  Views(6)  Comments(3Edit  收藏  举报