实验6

 1 def main():
 2     pass
 3 
 4 if __name__ == '__main__':
 5     main()
 6 '''
 7 把城市人口信息写入数据文件city1.csv中
 8 包括标题行
 9 使用python内置的文件操作实现
10 '''
11 title = ['城市', '人口(万)']
12 info = [ ['南京', '850'],
13 ['纽约', '2300'],
14 ['东京', '3800'],
15 ['巴黎', '1000'] ]
16 with open('city1.csv', 'w', encoding='utf-8') as f:
17     f.write(','.join(title) + '\n') # 写入标题行
18     for item in info:
19         f.write(','.join(item) + '\n') # 分行写入info

 

 

 

 

 

 

 

 

 

 

 1 def main():
 2     pass
 3 
 4 if __name__ == '__main__':
 5     main()
 6 '''
 7 从文件city.csv中读取城市、人口信息,打印输出到屏幕
 8 包括标题行
 9 使用python内置的文件操作实现
10 '''
11 with open('city1.csv','r',encoding = 'utf-8')as f:
12     print(f.read().rstrip('\n'))

 

 

 

 1 def main():
 2     pass
 3 
 4 if __name__ == '__main__':
 5     main()
 6 '''
 7 从文件city.csv中读取城市、人口信息,包括标题行
 8 将其保存到列表对象info中,保存形式诸如:
 9 info = [ ['城市', '人口(万)'],
10     ['南京', '850'],
11     ['纽约', '2300'],
12     ['东京', '3800'],
13     ['巴黎', '1000'] ]
14 在屏幕上打印输出
15 使用python内置的文件操作实现
16 '''
17 with open('city1.csv', 'r', encoding='utf-8') as f:
18     data = f.readlines()
19 # 打印中间处理结果(供查看)
20 print('data: ')
21 print(data)
22 info = [line.rstrip('\n').split(',') for line in data]
23 print('info: ')
24 print(info)

 

 

 

 

 1 def main():
 2     pass
 3 
 4 if __name__ == '__main__':
 5     main()
 6 '''
 7 把城市、人口信息写入数据文件city2.csv中
 8 包括标题行
 9 使用python内置的CSV模块实现
10 '''
11 import csv
12 title = ['城市', '人口(万)']
13 info = [ ['南京', '850'],
14    ['纽约', '2300'],
15    ['东京', '3800'],
16    ['巴黎', '1000'] ]
17 with open('city2.csv', 'w', encoding='utf-8', newline='') as f:
18     f_writer = csv.writer(f) # 为文件对象f创建一个writer对象
19     f_writer.writerow(title) # 通过writer对象的writerrow方法写入一行(标题行)
20     f_writer.writerows(info) # 通过writer对象的writerrows方法写入多行

 

 

 

 

 1 def main():
 2     pass
 3 
 4 if __name__ == '__main__':
 5     main()
 6 '''
 7 从文件city.csv中读取城市、人口信息,打印输出到屏幕
 8 包括标题行
 9 使用python内置的CSV模块实现
10 '''
11 import csv
12 with open('city2.csv', 'r', encoding='utf-8') as f:
13     f_reader = csv.reader(f)  # 为文件对象f创建一个reader对象
14     for line in f_reader:
15         print(line)

 

 

 

 1 def main():
 2     pass
 3 
 4 if __name__ == '__main__':
 5     main()
 6 '''
 7 把城市、人口信息以字典方式写入city3.csv文件
 8 使用python内置的CSV模块实现
 9 '''
10 import csv
11 with open('city3.csv', 'w', encoding='utf-8', newline='') as f:
12     title = ['城市', '人口']
13     f_writer = csv.DictWriter(f, fieldnames = title) # 为文件对象f创建一个DictWriter对象,并指定字段名称
14     f_writer.writeheader()
15     f_writer.writerow({'城市':'南京', '人口': '850万'})
16     f_writer.writerow({'城市':'纽约', '人口': '2300万'})
17     f_writer.writerow({'城市':'东京', '人口': '3800万'})
18     f_writer.writerow({'城市':'巴黎', '人口': '1000万'})

 

 

 

 1 def main():
 2     pass
 3 
 4 if __name__ == '__main__':
 5     main()
 6 '''
 7 从文件city3.csv中读取城市、人口信息
 8 使用python内置模块CSV实现
 9 '''
10 import csv
11 with open('city3.csv', 'r', encoding='utf-8') as f:
12     f_reader = csv.DictReader(f)  # 为文件对象f创建DictReader对象
13     for line in f_reader:
14         print(line)

 

 

 

 

 1 def is_valid(x):
 2     data=x
 3     if len(data)!=18:
 4         return False
 5     else:
 6         if ord(data[-1])==88 or 48<=ord(data[-1])<=57:
 7             return True
 8         else:
 9             return False
10 
11 with open('data3_id.txt','r',encoding = 'utf-8')as f:
12     data=f.readlines()
13     for i in data:
14         info = [line.rstrip('\n').split(',') for line in data]
15     m=info[0]
16     del info[0]
17     a=[]
18 for i in info:
19     if is_valid(i[1])==True:
20             a.append([i[0],i[1][6:10]+'-'+i[1][10:12]+'-'+i[1][12:14]])
21 a.sort(key=lambda x:x[1])
22 for i in a:
23     print(f'{i[0]},{i[1]}')

 

 

 1 import random
 2 with open('data5.txt','r',encoding = 'utf-8')as f:
 3     data=f.readlines()
 4     info=[line.rstrip('\n') for line in data]
 5     a = [line.split('\t') for line in info]
 6 
 7 import random
 8 a=random.sample(a, 5)
 9 for i in a:
10     print('{:<10}{:<10}{:30}'.format(i[0],i[1],i[2]))
11 import datetime
12 t=datetime.datetime.now()
13 
14 
15 with open(t.strftime('%Y%m%d') + '.txt', 'w',encoding = 'utf-8')as f:
16     for i in a:
17         f.write('{:<30}{:<20}{:<10}'.format(i[0],i[1],i[2])+'\n')

 

 

 1 import random
 2 with open('data5.txt','r',encoding = 'utf=8')as f:
 3     data=f.readlines()
 4     a=[line.rstrip('\n').split(',') for line in data]
 5 m='抽点开始'
 6 c=[]
 7 import random
 8 print(f'{m:=^40}')
 9 while True:
10     n=eval(input('入随机抽点人数:'))
11     b=random.sample(a, n)
12     for i in b:
13         a.remove(i)
14     if n>0:
15         for i in b:
16             print(i[0])
17             c.append(i[0])
18     else:
19         n='抽点结束'
20         print(f'{n:=^40}')
21         break
22 
23 import datetime
24 t=datetime.datetime.now()
25 
26 with open(t.strftime('%Y%m%d') + '.txt', 'w',encoding = 'utf-8')as f:
27     for i in c:
28         f.write(i+'\n')

 

posted @ 2022-05-24 21:24  lsqb27  阅读(8)  评论(3编辑  收藏  举报