Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Python Foundation

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the sample analysis on the basis of Python. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Foreword:

A few days ago, a fan named [- berry] asked a question about the Python of college students in the Python communication group, as shown in the following picture.

At first glance, I still feel a little difficult, for those who have just finished learning the basics, this small project is still a bit difficult. This topic mainly examines the basic operations such as random libraries, lists, dictionaries, open file operations, and so on. The use of real-life examples is indeed a good topic for practice.

First, train of thought

In fact, the key point of the problem is to construct the name, student number and grade, and then write it into the file in the form of a dictionary. Here we prepare two lists, one last name and one first name, and then use the random library to concatenate random strings to get the name.

2. Solution 1) TXT file storage

Go directly to the code, as follows:

#! / usr/bin/env python #-*-coding:utf-8-*-# @ FileName: test.py # @ Time: 21:34 # @ Author on 2021-9-19: Cat import random xing = [Zhao, Qian, Sun, Li, Zhou, Wu, Zheng, Wang, Feng, Chen, Yu, Wei, Jiang, Shen, Han, Yang, Zhu Qin, you, Xu, he, Lu, Shi, Zhang, Kong, Cao, Yan, Hua, Jin, Wei, Tao, Jiang, Qi, Xie, Zou, Yu, Bai, Shui, Dou, Zhang, Yun, Su, Pan, GE, Xi, Fan, Peng, Lang, Lu Wei, Chang, Ma, Miao, Feng, Hua, Fang, Yu, Ren, Yuan, Liu, Yu, Bao, Shi, Tang, Fei, Lian, Cen, Xue, Lei, he, Ni, Tang, Teng, Yin, Luo, Bi, Hao, Wu, an, Chang, Le Yu, Shi, Fu, Pi, Bian, Qi, Kang, Wu, Yu, Yuan, Bu, Gu, Meng, Ping, Huang, he, Mu, Xiao, Yin, Yao, Shao, Zhan, Wang, Qi, Mao, Yu, Di, Mi, Bei, Ming, Yi, Zhan 'Fu', 'Cheng', 'Dai', 'Tan', 'Song', 'Mao', 'Pang', 'Xiong','Ji', 'Shu','qu', 'item', 'Zhu', 'Dong', 'Liang',] ming = ['Fan', 'Jia', 'Qin', 'Zhen', 'Zhen', 'Hong', 'Huan', 'Feng', 'Lang', 'Hao', 'Liang', 'Zheng', 'Qian', 'Zhen', 'Hong', 'Huan', 'Feng', 'Lang', 'Hao', 'Liang', 'Zheng', 'Qian', 'Zhen' Zhuang, Wei, Gang, Yong, Yi, Jun, Feng, Qiang, Jun, Ping, Bao, Dong, Wen, Hui, Li, Gu, Zhi, Duan, Dian, Tai, Li, Qing, Fei, Bin, Fu, Shun, Xin, Zi, Jie, Tao, Chang, Cheng 'Kang', 'Xing', 'Han', 'Cheng','Bo', 'first', 'respect', 'Ruo', 'Ming', 'friend', 'Bin', 'Liang', 'Dong', 'Wei','Qi','Ke', 'Lun', 'Xiang','Xu', 'Peng','Ze', 'Lang','Bo', 'Biao', 'Jin', 'Sheng', 'Cheng', 'first', 'respect', 'Zhen', 'Zhen', 'Zhuang' 'Hui','Si', 'Qun', 'Hao', 'Xin', 'Bang', 'Cheng','Le', 'Hong', 'Yan','Xi', 'Yan', 'Hao', 'Guang', 'Tian','da','an', 'Yan', 'Zhong', 'Mao', 'Jin', 'Lin', 'you', 'Jian', 'and' Biao','Bo', 'Tai', 'Sheng', 'Zhen', 'quite', 'switch' Ming, Yong, Jian, Shi, Guang, Zhi, Yi, Xing, Liang, Hai, Shan, Jen, Bo, Ning, Xing, Shi, Zhi, Zhi, Si, Shao, Gong, Song, Shan, Hou, Qing, Lei, Min, Friends, Yu, he, Zhe, Jiang Chao, Yan, de, Zhang, Zheng, Law, Chen, Chen, Shi, Yi, Jian, Jia, Zhi, Yu, Xuan, Yan, Bo, Ning, Gui, Fu, Sheng, long, Yuan, Quan, Guo, Sheng, Xue, Xiang, Cai, Fa, Wu, Xin 'Li', 'Qing', 'Fei', 'Bin','Fu', 'Shun', 'Xin','Zi', 'Jie', 'Tao', 'Chang', 'Cheng', 'Kang', 'Xing', 'Guang', 'Tian','da','an', 'Yan', 'Zhong', 'Mao', 'Jin', 'Lin', 'you', 'Jian', 'and'] with open ('students.txt',' a' Encoding='utf-8') as f: for i in range (9): item = {} item ['name'] = random.choice (xing) + random.choice (ming) item ['student number'] = str (0100 + I) item ['grade'] = random.randint (0,100) f.write (str (item)) f.write ('\ n') 2) CSV file storage

The code is as follows:

#! / usr/bin/env python #-*-coding:utf-8-*-# @ FileName: test_csv.py # @ Time: 21:01 # @ Author on 2021-9-26: Cat import random import csv headers = ['name', 'student number', 'grade'] xing = ['Zhao', 'Qian', 'Sun','Li', 'Zhou','Wu', 'Zheng', 'Wang', 'Feng', 'Chen' 'Fu', 'Wei', 'Jiang', 'Shen', 'Han', 'Yang', 'Zhu', 'Qin', 'you','Xu','he','Lu', 'Shi', 'Zhang', 'Kong', 'Cao', 'Yan', 'Hua', 'Jin', 'Wei', 'Tao', 'Jiang','Qi', 'Xie', 'Zou','Yu', 'Bai', 'Shui', 'Dou', 'Zhang', 'Yun','Su' Pan, GE, Xi, Fan, Peng, Lang, Lu, Wei, Chang, Ma, Miao, Feng, Hua, Fang, Yu, Ren, Yuan, Liu, Yu, Bao, Shi, Tang, Fei, Lian, Cen, Xue, Lei, he, Ni, Tang, Teng, Yin 'Luo','Bi', 'Hao','Wu','an', 'Chang','Le','Yu', 'Shi','Fu','Pi', 'Bian','Qi', 'Kang','Wu','Yu', 'Yuan','bu','Gu', 'Meng', 'Ping', 'Huang','Mu', 'Xiao', 'Yin', 'Yao', 'Shao', 'Zhan', 'Wang','Qi', 'Mao' Yu, Di, mi, Bei, Ming, Yu, Ji, Fu, Cheng, Dai, Tan, Song, Mao, Pang, Xiong, Ji, Shu, qu, Zhu, Dong, Liang,] ming = [Fan, Jia, Qin, Zhen, Zhen, Hong, Huan, Feng Long, Hao, Liang, Zheng, Qian, Zhen, Zhuang, Wei, Gang, Yong, Yi, Jun, Feng, Qiang, Jun, Ping, Bao, Dong, Wen, Hui, Li, Gu, Zhi, Duan, Dian, Tai, Li, Qing, Fei, Bin, Fu, Shun 'xin','Zi', 'Jie', 'Tao', 'Chang', 'Cheng', 'Kang', 'Xing', 'John', 'Cheng','Bo', 'first', 'respect', 'Ruo', 'Ming', 'friend', 'bin', 'Liang', 'Dong', 'Wei','Qi','Ke', 'Lun', 'Xiang','Xu', 'Peng','ze', 'Lang','Bo', 'Biao', 'Jin', 'Sheng' 'Cheng', 'Xian', 'Jingjing', 'Zhen', 'Zhen', 'Zhuang', 'Hui','Si', 'Group', 'Hao', 'Xin', 'Bang', 'Cheng','Le', 'Hong', 'Yan', 'Yan','Yi', 'Hao', 'Guang', 'Heaven','Da','an', 'Yan', 'Zhong', 'Mao', 'Jin', 'Lin', 'you', 'firm', 'and' Biao' 'Bo', 'Tai', 'Sheng', 'Zhen', 'Ting', 'switch', 'Ming', 'Yong', 'Jian', 'Shi', 'Guang', 'Zhi','Yi', 'Xing', 'good', 'Hai', 'Shan', 'Ren','Bo', 'Ning', 'Xing', 'Shi', 'Zhi', 'Zhong', 'think', 'Shao', 'Gong', 'Song', 'good', 'thick', 'Qing', 'Lei' 'min', 'you','Yu','he', 'Zhe', 'Jiang', 'Chao', 'Yan', 'virtue', 'Zhong', 'Zheng', 'law', 'morning', 'Chen', 'Shi','Yi', 'Jian', 'Jia', 'Zhi','Yu', 'Xuan', 'Yan','Bo', 'Ning', 'Gui','Fu', 'Sheng', 'long', 'Yuan', 'Quan', 'Guo', 'Sheng' 'Xue', 'Xiang', 'Cai','Fa','Wu', 'Xin','Li', 'Qing', 'Fei', 'Bin','Fu', 'Shun', 'Xin','Zi', 'Jie', 'Tao', 'Chang', 'Cheng', 'Kang', 'Xing', 'Light', 'Heaven','Da','an', 'Yan', 'Zhong', 'Mao', 'Jin', 'Lin', 'you', 'Jian' 'and'] with open ('students.csv',' asides, encoding='utf-8', newline='') as f: csv_writer = csv.DictWriter (f Headers) csv_writer.writeheader () for i in range (9): item = {} item ['name'] = random.choice (xing) + random.choice (ming) item ['student number'] = str (0100 + I) item ['grade'] = random.randint (0100) csv_writer.writerow (item)

After each run of the program, the contents of the txt and csv files are randomly obtained to complete the requirements.

This is the end of the sample analysis on the basis of Python. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report