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

How to deal with data in Python Application of csv

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I would like to share with you how Python data processing csv application related knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you will learn something after reading this article, let's take a look at it.

Title

The document scores.csv contains the transcripts of ten students with the heading "name, Chinese, Mathematics and English". Please program to complete the following functions.

1) calculate the total score and ranking of each student, and write the expanded student information into the file data.csv. The new file header is "name, Chinese, Mathematics and English Total score ranking".

2) at the same time, the branch outputs the highest and lowest scores of each course and the corresponding student names on the console, and the output format is "course name: (highest score, student 1,., student n), (lowest score, student 1,... student n)"

3) if the total score is the same, there may be many people juxtaposed in the same rank, and if the highest or lowest score is the same, so many people will be written in the same tuple in alphabetical order.

Code fr=open ("scores.csv", "r") fw=open ("data.csv", "w") ls= [] for line in fr: line=line.replace ("\ n", "") ls.append (line.split (",")) ChMax= [0SCH'] ChMin= [100LING'] MaMax= [0LQ'] MaMin= [100MIT'] EnMax= [0MIT'] EnMin= [100MIT'] # when the function has list or dictionary as a formal parameter Will change the value of the actual parameter (if you change its value in the function) # but if you do not want the value of the argument to be affected, you can use the [] .copy method when passing the parameter. Both list and dictionary have copy methods. # def updateMaxMin (Max,Min,score,i): # if (score > Max [0]): # Max= [0jurisdiction'] # Max [0] = score# Max [1] = ls [I] [0] # elif (score==Max [0]): # Max.append (LS [I] [0]) # if (score ChMax [0]): ChMax = [0] ''] ChMax [0] = chScore ChMax [1] = ls [I] [0] elif (chScore = = ChMax [0]): ChMax.append (LS [I] [0]) if (chScore

< ChMin[0]): ChMin = [100, ''] ChMin[0] = chScore ChMin[1] = ls[i][0] elif (chScore == ChMin[0]): ChMin.append(ls[i][0]) #数学最高分最低分更新 maScore=eval(ls[i][2]) if (maScore >

MaMax [0]): MaMax = [0,'] MaMax [0] = maScore MaMax [1] = ls [I] [0] elif (maScore = = MaMax [0]): MaMax.append (LS [I] [0]) if (maScore

< MaMin[0]): MaMin = [100, ''] MaMin[0] = maScore MaMin[1] = ls[i][0] elif (maScore == MaMin[0]): MaMin.append(ls[i][0]) #英语最高分最低分更新 enScore=eval(ls[i][3]) if (enScore >

EnMax [0]): EnMax = [0,'] EnMax [0] = enScore EnMax [1] = ls [I] [0] elif (enScore = = EnMax [0]): EnMax.append (ls [I] [0]) if (enScore < EnMin [0]): EnMin = [100] ''] EnMin [0] = enScore EnMin [1] = ls [I] [0] elif (enScore = = EnMin [0]): EnMin.append (ls[ I] [0]) # sorts each row in the 2D list from highest to lowest total score # here I use bubbling sort for i in range (1Llen (ls): for j in range (iFe1Len (ls)): if ls [I] [4]

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