实验5

 

 

 1 # task1_1.py
 2 # 统计文件data1_1.txt行数 (不包括空白行)
 3 # data1_1.txt中的空白行都是由\n构成的空白行
 4 with open('data1_1.txt', 'r', encoding = 'utf-8') as f:
 5     data = f.readlines() # 一次读入所有数据,按行读取,保存到列表中
 6 n = 0
 7 for line in data:
 8     if line.strip('\n') == '':
 9         continue
10     n += 1
11 print(f'共{n}行')

 

 

 1 # task1_2.py
 2 # 统计文件data1_1.txt行数 (不包括空白行)
 3 # data1_1.txt中的空白行都是由\n构成的空白行
 4 with open('data1_1.txt', 'r', encoding = 'utf-8') as f:
 5     n = 0
 6     for line in f: # 逐行遍历处理
 7         if line.strip('\n') == '':
 8             continue
 9         n += 1
10 print(f'共{n}行')

 

 

 1 # task1_3.py
 2 # 统计文件data1_2.txt行数 (不包括空白行)
 3 # data1_2.txt中的空白行包括由空格、Tab键(\t)、换行(\n)构成的空白行
 4 with open('data1_2.txt', 'r', encoding = 'utf-8') as f:
 5     n = 0
 6     for line in f:
 7         if line.strip() == '':
 8             continue
 9         n += 1
10 print(f'共{n}行')

 

 

 1 # task1_3.py
 2 # 统计文件data1_2.txt行数 (不包括空白行)
 3 # data1_2.txt中的空白行包括由空格、Tab键(\t)、换行(\n)构成的空白行
 4 with open('data1_2.txt', 'r', encoding = 'utf-8') as f:
 5     n = 0
 6     for line in f:
 7         if line.strip() == '':
 8             continue
 9         n += 1
10 print(f'共{n}行'

 

 1 # task1_4.py
 2 # 统计文件data1_2.txt行数 (不包括空白行)
 3 # data1_2.txt中的空白行包括由空格、Tab键(\t)、换行(\n)构成的空白行
 4 with open('data1_2.txt', 'r', encoding = 'utf-8') as f:
 5     n = 0
 6     for line in f:
 7         if line.isspace():
 8             continue
 9         n += 1
10 print(f'共{n}行')

 

 

 实验2

 1 # 统计其中只出现过一次的独特行行数,在屏幕上打印输出结果。
 2 # task2.py
 3 with open('data2.txt', 'r', encoding = 'utf-8') as f:
 4     data = f.read().split('\n')
 5 unique_line = []
 6 for line in data:
 7     if data.count(line) == 1:
 8         unique_line.append(line)
 9 print(f'共{ len(unique_line) }独特行')
10 for i in unique_line:
11     print(i)

 

 1 with open('data3.txt', 'r', encoding = 'utf-8') as f:
 2     a=f.read().split("\n")
 3 l=[]
 4 l.append('四舍五入数据')
 5 for i in range(1,len(a)):
 6     l.append(round(float(a[i])))
 7     a[i]=str(round(float(a[i]),1))
 8 with open('data3_processed.txt', 'w+', encoding = 'utf-8') as f:
 9     f.write(a[0]+'\t'+str(l[0]))
10     f.write("\n")
11     for i in range(1,len(a)):
12         f.write(str(a[i]))
13         f.write('\t'*2+str(l[i])) if len(a[i])>=4 else f.write('\t'*2+str(l[i]))
14         f.write('\n')
15 for i in range(1,len(a)):
16     a[i]=float(a[i])
17 print(a[0],'\n'+str(a[1:]))
18 print(l[0],'\n'+str(l[1:]))

 

 

 1 with open('data4.txt','r+',encoding='utf-8') as f:
 2     str=[i.strip('\n').split('\t') for i in f]
 3     a=str.pop(0)
 4     str.sort(key=lambda x : (x[2],-int(x[-1])))
 5     print('\t'.join(a))
 6     for i in str:
 7         print('\t'.join(i))
 8 with open('data4_progressed.txt','w') as f:
 9     f.write('\t'.join(a))
10     f.write('\n')
11     for i in str:
12         f.write('\t'.join(i))
13         f.write('\n')

 

 

 1 with open('data5.txt', 'r+', encoding = 'utf-8') as f:
 2     data=f.read()
 3     print('行数',len(data.splitlines()))
 4     print('单词数',len(data.split()))
 5     s=0
 6     for i in data:
 7         if i.isspace():
 8             s=s+1
 9     print('空格数',s)
10     print('字符数',len(data))
11 with open('data5_with_line.txt', 'w+', encoding = 'utf-8') as f:
12     for i in range(len(data.splitlines())):
13         f.write(str(i+1)+' '+str(data.splitlines()[i])+'\n')

 

posted @ 2022-05-16 23:40  lsqb27  阅读(6)  评论(3编辑  收藏  举报