Preface:
The other day a fan named [-berry] asked a question about Python topics for college students in a Python exchange group, as shown below.
At first glance, I still think it is a bit difficult, for those who have just finished learning the basics, this small project is still a bit difficult, this topic mainly examines the random library, lists, dictionaries, open file operations and other basic operations, the use of real-life examples, it is really a good topic to practice.
I. Thoughts
In fact, the key point of the problem lies in constructing the name, number and grade, after which the file is written in the form of a dictionary. Here we prepare two lists, one for the last name and one for the first name, and then use therandom
The library performs random string splicing to get the name.
II. Solutions
1) TXT file storage
Straight to the code, as shown below:
# !/usr/bin/env python # -*- coding:utf-8 -*- # @FileName : # @Time :2021/9/19 21:34 # @Author :Cat import random xing = ['Zhao','Money','Sun','Lee','Chow','Wu','Zheng','King', 'Feng','Chen','Chu','Wei','Chiang','Shen','Han','Yang', 'Zhu','Qin','Yo','Permit','Ho','Lui','Shi','Zhang', 'Hole','Cao','Strict','Hua','Gold','Wei','Tao','Ginger', 'Chi','Thank you','Zou','Metaphor','Cypress','Water','Sinus','Chapter', 'Cloud','Su','Pan','Kudzu','Xi','Fan','Peng','Lang', 'Lu','Wei','Chang','Horse','Miao','Phoenix','Flower','Square', 'Yu','Any','Yuan','Willow','Feng','Bao','History','Tang', 'Fee','Lian','Cen','Xue','Thunder','He','Ni','Soup', 'Teng','Yin','Lo','Bi','Hao','Wu','An','constant', 'Joy','in','Time','Fu','Pi','Bian','Qi','Kang', 'Woo','Yu','Yuan','Divination','Gu','Meng','Ping','Yellow', ' and ','Mu','Xiao','Yin','Yao','Shao','Zhan','Woof', 'Qi','Gross','Yu','Di','Rice','Bay','Ming','Zang', 'Counting','Volt','Success','Dai','Talk','Song','Mao','Pon', 'Bear','discipline','Shu','Qu','item','Wish','Dong','Liang',] ming = [ 'Where','Good','Diligent','Precious','Chastity','Hong','Huan','Wind','Lang', 'Ho','Bright','Politics','Humble','vibrate','Strong','Great','Just','Brave','Yi','Jun','Peak','Strong','Army','Ping','Bao','East','Man','Fai','Force','Solid','of','Paragraph','Temple','Tai', 'Profit','Ching','Fly','Bin','Rich','Shun','Faith','Son','Jai','Tao','Chang','Success','Kang','Star','Han','Makoto','Bo','First','Respect', 'If','Tweet','Pals','Bin','Liang','Building','Dimension','Enlightenment','K','Len','Sho','Rising Sun','Peng','Ze','Lang','Bur','Biao','Jin','Sung', 'Makoto','First','Respect','Shock','vibrate','Strong','will','Think','swarm','How','Heart','Bang','Bear','Joy','Macro','Words','Sommelier','Min','Hao', 'Light','Heaven','Da','An','Rock','Medium','Mau','In','Lin','There','Firm',' and ','Biao','Bo','Tai','Sheng','vibrate','Stick it','Switch', 'Ming','Forever','Healthy','World','Wide','Zhi','Righteousness','Hing','good','Sea','Mountain','Jen','Wave','Ning','line','Time','Zhi','Loyalty','Think', 'Introduction','Kung Fu','Loose','Good','Thick','Celebration','Lei','People','Friends','Yu','The River','Philosophy','River','Super','Inflammation','Virtue','Recognize','levy','Law', 'Morning','Tatsu','Shi','by','Build','Home','To','Fabulous','Xuan','Inflammation','Wave','Ning','Noble','Blessed','Born','Dragon','Yuan','All','State', 'Sheng','Learning','Xiang','Talent','Hair','Martial','New','Profit','Ching','Fly','Bin','Rich','Shun','Faith','Son','Jai','Tao','Chang','Success', 'Kang','Star','Light','Heaven','Da','An','Rock','Medium','Mau','In','Lin','There','Firm',' and ' ] with open('', 'a', encoding='utf-8') as f: for i in range(9): item = {} item['Name'] = (xing) + (ming) item['School Number'] = str(2020010001 + i) item['Achievements'] = (0, 100) (str(item)) ('\n')
After running it, the result obtained is shown below:
2) CSV file storage
The code is shown below:
# !/usr/bin/env python # -*- coding:utf-8 -*- # @FileName :test_csv.py # @Time :2021/9/26 21:01 # @Author :Cat import random import csv headers = ['Name', 'School Number', 'Achievements'] xing = ['Zhao','Money','Sun','Lee','Chow','Wu','Zheng','King', 'Feng','Chen','Chu','Wei','Chiang','Shen','Han','Yang', 'Zhu','Qin','Yo','Permit','Ho','Lui','Shi','Zhang', 'Hole','Cao','Strict','Hua','Gold','Wei','Tao','Ginger', 'Chi','Thank you','Zou','Metaphor','Cypress','Water','Sinus','Chapter', 'Cloud','Su','Pan','Kudzu','Xi','Fan','Peng','Lang', 'Lu','Wei','Chang','Horse','Miao','Phoenix','Flower','Square', 'Yu','Any','Yuan','Willow','Feng','Bao','History','Tang', 'Fee','Lian','Cen','Xue','Thunder','He','Ni','Soup', 'Teng','Yin','Lo','Bi','Hao','Wu','An','constant', 'Joy','in','Time','Fu','Pi','Bian','Qi','Kang', 'Woo','Yu','Yuan','Divination','Gu','Meng','Ping','Yellow', ' and ','Mu','Xiao','Yin','Yao','Shao','Zhan','Woof', 'Qi','Gross','Yu','Di','Rice','Bay','Ming','Zang', 'Counting','Volt','Success','Dai','Talk','Song','Mao','Pon', 'Bear','discipline','Shu','Qu','item','Wish','Dong','Liang',] ming = [ 'Where','Good','Diligent','Precious','Chastity','Hong','Huan','Wind','Lang', 'Ho','Bright','Politics','Humble','vibrate','Strong','Great','Just','Brave','Yi','Jun','Peak','Strong','Army','Ping','Bao','East','Man','Fai','Force','Solid','of','Paragraph','Temple','Tai', 'Profit','Ching','Fly','Bin','Rich','Shun','Faith','Son','Jai','Tao','Chang','Success','Kang','Star','Han','Makoto','Bo','First','Respect', 'If','Tweet','Pals','Bin','Liang','Building','Dimension','Enlightenment','K','Len','Sho','Rising Sun','Peng','Ze','Lang','Bur','Biao','Jin','Sung', 'Makoto','First','Respect','Shock','vibrate','Strong','will','Think','swarm','How','Heart','Bang','Bear','Joy','Macro','Words','Sommelier','Min','Hao', 'Light','Heaven','Da','An','Rock','Medium','Mau','In','Lin','There','Firm',' and ','Biao','Bo','Tai','Sheng','vibrate','Stick it','Switch', 'Ming','Forever','Healthy','World','Wide','Zhi','Righteousness','Hing','good','Sea','Mountain','Jen','Wave','Ning','line','Time','Zhi','Loyalty','Think', 'Introduction','Kung Fu','Loose','Good','Thick','Celebration','Lei','People','Friends','Yu','The River','Philosophy','River','Super','Inflammation','Virtue','Recognize','levy','Law', 'Morning','Tatsu','Shi','by','Build','Home','To','Fabulous','Xuan','Inflammation','Wave','Ning','Noble','Blessed','Born','Dragon','Yuan','All','State', 'Sheng','Learning','Xiang','Talent','Hair','Martial','New','Profit','Ching','Fly','Bin','Rich','Shun','Faith','Son','Jai','Tao','Chang','Success', 'Kang','Star','Light','Heaven','Da','An','Rock','Medium','Mau','In','Lin','There','Firm',' and ' ] with open('', 'a', encoding='utf-8', newline='') as f: csv_writer = (f, headers) csv_writer.writeheader() for i in range(9): item = {} item['Name'] = (xing) + (ming) item['School Number'] = str(2020010001 + i) item['Achievements'] = (0, 100) csv_writer.writerow(item)
After running it, the result obtained is shown below:
After each run of the program, the contents in the txt and csv files obtained are randomly fetched, completing the requirements.
III. Summary
I am a Python Advanced. This article is based on a fan question, complete with a Python basic practical question, which covers the main knowledge points ofrandom
Knowledge of libraries, lists, dictionaries, file reading, etc.
To this point this article on the Python basic combat summary of the article is introduced to this, more related to the Python basic combat content please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!