实验6 文件应用编程-2

2022/5/23 1:05:42

本文主要是介绍实验6 文件应用编程-2,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.实验任务1:CSV格式文件读写

task1_1.py

1 title = ['城市', '人口(万)']
2 info = [ ['南京', '850'],
3 ['纽约', '2300'],
4 ['东京', '3800'],
5 ['巴黎', '1000'] ]
6 with open('city1.csv', 'w', encoding='utf-8') as f:
7     f.write(','.join(title) + '\n') 
8     for item in info:
9         f.write(','.join(item) + '\n')     

 

task1_2.py

1 with open('city1.csv', 'r', encoding='utf-8') as f:
2     print(f.read().rstrip('\n'))

 

task1_3.py

1 with open('city1.csv', 'r', encoding='utf-8') as f:
2     data = f.readlines()
3 print('data: ')
4 print(data)
5 info = [line.rstrip('\n').split(',') for line in data]
6 print('info: ')
7 print(info)

 

2.实验任务2

task2_1.py

 1 import csv
 2 title = ['城市', '人口(万)']
 3 info = [ ['南京', '850'],
 4 ['纽约', '2300'],
 5 ['东京', '3800'],
 6 ['巴黎', '1000'] ]
 7 with open('city2.csv', 'w', encoding='utf-8', newline='') as f:
 8     f_writer = csv.writer(f)
 9     f_writer.writerow(title)
10     f_writer.writerows(info)

 

task2_2.py

1 import csv
2 with open('city2.csv', 'r', encoding='utf-8') as f:
3     f_reader = csv.reader(f)
4     for line in f_reader:
5         print(line)

 

 

task2_3.py

1 import csv
2 with open('city3.csv', 'w', encoding='utf-8', newline='') as f:
3     title = ['城市', '人口']
4     f_writer = csv.DictWriter(f, fieldnames = title)
5     f_writer.writeheader()
6     f_writer.writerow({'城市':'南京', '人口': '850万'})
7     f_writer.writerow({'城市':'纽约', '人口': '2300万'})
8     f_writer.writerow({'城市':'东京', '人口': '3800万'})
9     f_writer.writerow({'城市':'巴黎', '人口': '1000万'})

 

 

task2_4.py

1 import csv
2 with open('city3.csv', 'r', encoding='utf-8') as f:
3     f_reader = csv.DictReader(f)
4     for line in f_reader:
5         print(line)

 

 

3.实验任务3身份证号码文件批量处理

 1 with open('data3_id.txt', 'r', encoding='utf-8') as f:
 2     data=f.readlines()
 3 data=[line.strip().split(',') for line in data]
 4 ans=[]
 5 def isvalid(id):
 6     if len(id)!=18: return False
 7     else:
 8         for i in id:
 9             if '0'<=i<='9'or i=='X':continue
10             return False
11         return True
12 for i in range(1,len(data)):
13     if(isvalid(data[i][1])):
14         ans.append([data[i][0],data[i][1][6:10]+'-'+data[i][1][10:12]+'-'+data[i][1][12:14]])
15 ans.sort(key=lambda x:x[1])
16 print(f'{data[0][0]},{data[0][1]}')
17 for line in ans:
18     print(f'{line[0]},{line[1]}')

 

 

4.实验任务4

 

 5.实验任务5

task5_1.py

 1 import random
 2 
 3 n = int(input("输入随机抽点人数: "))
 4 with open("data5.txt", encoding="utf-8") as f:
 5     data = f.readlines()
 6 data_ = data.copy()
 7 lucky_people = []
 8 for i in range(n):
 9     x = random.choice(data_)
10     lucky_people.append(x)
11     data_.remove(x)
12 with open("20220517.txt", "w+", encoding="utf-8") as f:
13     f.writelines(lucky_people)
14     f.seek(0)
15     print(f.read())

 

 

task5_2.py

 1 import random
 2 
 3 print("{0:{1}{3}{2}}".format("抽点开始", "=", 40, "^"))
 4 active_input = True
 5 ls = []
 6 while active_input:
 7     n = int(input("输入随机抽点人数: "))
 8     if n == 0:
 9         break
10     else:
11         with open("data5.txt", encoding="utf-8") as f:
12             data = f.readlines()
13         data_ = data.copy()
14         lucky_people = []
15         for i in range(n):
16             x = random.choice(data_)
17             lucky_people.append(x)
18             data_.remove(x)
19         with open("20220517.txt", "w+", encoding="utf-8") as f:
20             f.writelines(lucky_people)
21             f.seek(0)
22             print(f.read())
23     ls += lucky_people
24 with open("20220517.txt", "w", encoding="utf-8") as f:
25     f.writelines(ls)
26 print("{0:{1}{3}{2}}".format("抽点结束", "=", 40, "^"))

 

6.实验任务6

1 import os
2 print(os.getcwd())
3 py_file_list = [ file for file in os.listdir() if file.endswith('.py') ]
4 for number, file in enumerate(py_file_list, 1):
5     print(f'{number:-3d}. {file}')

 



这篇关于实验6 文件应用编程-2的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程