姓名,学号,C语言,Java,Python,VB,C++,总分朱佳,0121701100511,75.2,93,66,85,88,407李思,0121701100513,86,76,96,93,67,418郑君,0121701100514, ,98,76, ,89,263王雪,0121701100515,99,96,91,88,86,460罗明,0121701100510,95,96,85,63,91,430
- 计算总分
 
每次输入一个分数,重复10次,计算总分和平均分
total = 0for i in range(10):score = float(input())total = total + scoreprint(total)print(total / 10)
输入89.55625897810065883699输出725.572.55
在一行内输入用空格分隔的多个成绩,计算总分和平均分
score = list(map(float, input().split(',')))# score = eval(input()) # 输入用逗号分隔的数值时可以用eval转为元组print(sum(score))print(sum(score) / len(score))
75.2,93,66,85,88407.281.44
2.字符串切片
score = '朱佳,0121701100511,75.2,93,66,85,88,407'print(score[17:]) # 切片获得成绩部分数据'75.2,93,66,85,88,407'print(score[17:-4]) # 切片获得各门课程数据'75.2,93,66,85,88'print(sum(eval(score[17:-4]))) # eval将字符串转元组,sum()对元组求和,407.2
3.字符串切分
score = '朱佳,0121701100511,75.2,93,66,85,88,407'score_ls = score.split(',')print(score_ls)# ['朱佳', '0121701100511', '75.2', '93', '66', '85', '88', '407']print(score_ls[2:-1]) # ['75.2', '93', '66', '85', '88']print(sum(map(float,score_ls[2:-1]))) # 407.2print(sum(map(float,score_ls[2:-1]))/len(score_ls[2:-1])) # 81.44
- 遍历文件8.5 score.csv
姓名,学号,C语言,Java,Python,VB,C++,总分朱佳,0121701100511,75.2,93,66,85,88,407李思,0121701100513,86,76,96,93,67,418郑君,0121701100514, ,98,76, ,89,263王雪,0121701100515,99,96,91,88,86,460罗明,0121701100510,95,96,85,63,91,430
```python 姓名,学号,C语言,Java,Python,VB,C++,总分# 读文件,逐行输出with open('../data/csv/8.5 score.csv','r',encoding='utf-8') as f:for line in f: # 遍历文件对象,依次获取文件的一行,字符串类型print(line) # 输出当前行,行末有换行符# '姓名,学号,C语言,Java,Python,VB,C++,总分\n'
 
朱佳,0121701100511,75.2,93,66,85,88,407
李思,0121701100513,86,76,96,93,67,418
郑君,0121701100514, ,98,76, ,89,263
王雪,0121701100515,99,96,91,88,86,460
罗明,0121701100510,95,96,85,63,91,430
```python# 读文件,去掉行末换行符with open('../data/csv/8.5 score.csv','r',encoding='utf-8') as f:for line in f: # 遍历文件对象,依次获取文件的一行,字符串类型print(line.strip()) # 输出当前行,行末有换行符# '姓名,学号,C语言,Java,Python,VB,C++,总分\n'
姓名,学号,C语言,Java,Python,VB,C++,总分
朱佳,0121701100511,75.2,93,66,85,88,407
李思,0121701100513,86,76,96,93,67,418
郑君,0121701100514, ,98,76, ,89,263
王雪,0121701100515,99,96,91,88,86,460
罗明,0121701100510,95,96,85,63,91,430
# 读文件,每行输出为一个列表
with open('../data/csv/8.5 score.csv','r',encoding='utf-8') as f:
    for line in f:   # 遍历文件对象,依次获取文件的一行,字符串类型
        print(line.strip().split(','))  # 输出当前行,行末有换行符
        # ['姓名', '学号', 'C语言', 'Java', 'Python', 'VB', 'C++', '总分']
['姓名', '学号', 'C语言', 'Java', 'Python', 'VB', 'C++', '总分']
['朱佳', '0121701100511', '75.2', '93', '66', '85', '88', '407']
['李思', '0121701100513', '86', '76', '96', '93', '67', '418']
['郑君', '0121701100514', ' ', '98', '76', ' ', '89', '263']
['王雪', '0121701100515', '99', '96', '91', '88', '86', '460']
['罗明', '0121701100510', '95', '96', '85', '63', '91', '430']
# 读文件,拼接为一个列表
with open('../data/csv/8.5 score.csv','r',encoding='utf-8') as f:
    score = []
    for line in f:   # 遍历文件对象,依次获取文件的一行,字符串类型
        score = score + line.strip().split(',')  # 输出当前行,行末有换行符
print(score)
['姓名', '学号', 'C语言', 'Java', 'Python', 'VB', 'C++', '总分', '朱佳', '0121701100511', '75.2', '93', '66', '85', '88', '407', '李思', '0121701100513', '86', '76', '96', '93', '67', '418', '郑君', '0121701100514', ' ', '98', '76', ' ', '89', '263', '王雪', '0121701100515', '99', '96', '91', '88', '86', '460', '罗明', '0121701100510', '95', '96', '85', '63', '91', '430']
# 读文件,拼接为一个列表,用切片方法获取各列数据
with open('../data/csv/8.5 score.csv','r',encoding='utf-8') as f:
    score = []
    for line in f:   # 遍历文件对象,依次获取文件的一行,字符串类型
        score = score + line.strip().split(',')  # 输出当前行,行末有换行符
print(score[::8])   # ['姓名', '朱佳', '李思', '郑君', '王雪', '罗明']
print(score[4::8])  # ['Python', '66', '96', '76', '91', '85']
print(score[7::8])  # ['总分', '407', '418', '263', '460', '430']
5.读文件转二维列表
with open('../data/csv/8.5 score.csv','r',encoding='utf-8') as f:
    score = []
    for line in f:   # 遍历文件对象,依次获取文件的一行,字符串类型
        score.append(line.strip().split(','))  # 输出当前行,行末有换行符
print(score)
# 用列表推导式实现
with open('../data/csv/8.5 score.csv','r',encoding='utf-8') as f:
    score = [line.strip().split(',') for line in f]
print(score)
[['姓名', '学号', 'C语言', 'Java', 'Python', 'VB', 'C++', '总分'], 
 ['朱佳', '0121701100511', '75.2', '93', '66', '85', '88', '407'], 
 ['李思', '0121701100513', '86', '76', '96', '93', '67', '418'], 
 ['郑君', '0121701100514', ' ', '98', '76', ' ', '89', '263'], 
 ['王雪', '0121701100515', '99', '96', '91', '88', '86', '460'], 
 ['罗明', '0121701100510', '95', '96', '85', '63', '91', '430']]
# 用推导式从二维列表中获取各列数据
with open('../data/csv/8.5 score.csv','r',encoding='utf-8') as f:
    score = [line.strip().split(',') for line in f]
print(score)
user = [x[0] for x in score]
print(user)  # ['姓名', '朱佳', '李思', '郑君', '王雪', '罗明']
python = [x[4] for x in score]
print(python)  # ['Python', '66', '96', '76', '91', '85']
total = [x[-1] for x in score]
print(total)   # ['总分', '407', '418', '263', '460', '430']
6.读文件转字典
score = []
with open('../data/csv/8.5 score.csv','r',encoding='utf-8') as f:
    title = f.readline().strip().split(',')
    for line in f:
        ls = line.strip().split(',')
        dic = dict(zip(title,ls))
        score.append(dic)
print(score)
[
 {'姓名': '朱佳', '学号': '0121701100511', 'C语言': '75.2', 'Java': '93', 'Python': '66', 'VB': '85', 'C++': '88', '总分': '407'}, 
 {'姓名': '李思', '学号': '0121701100513', 'C语言': '86', 'Java': '76', 'Python': '96', 'VB': '93', 'C++': '67', '总分': '418'}, 
 {'姓名': '郑君', '学号': '0121701100514', 'C语言': ' ', 'Java': '98', 'Python': '76', 'VB': ' ', 'C++': '89', '总分': '263'}, 
 {'姓名': '王雪', '学号': '0121701100515', 'C语言': '99', 'Java': '96', 'Python': '91', 'VB': '88', 'C++': '86', '总分': '460'}, 
 {'姓名': '罗明', '学号': '0121701100510', 'C语言': '95', 'Java': '96', 'Python': '85', 'VB': '63', 'C++': '91', '总分': '430'}
 ]
- pandas读文件 ```python import pandas as pd
 
score = pd.read_csv(‘../data/csv/8.5 score.csv’) print(score) # dataframe 格式
```python
   姓名            学号   C语言  Java  Python  VB  C++   总分
0  朱佳  121701100511  75.2    93      66  85   88  407
1  李思  121701100513    86    76      96  93   67  418
2  郑君  121701100514          98      76       89  263
3  王雪  121701100515    99    96      91  88   86  460
4  罗明  121701100510    95    96      85  63   91  430
import pandas as pd
score = pd.read_csv('../data/csv/8.5 score.csv')
print(score.columns.tolist())  # 列表,['姓名', '学号', 'C语言', 'Java', 'Python', 'VB', 'C++', '总分']
print(score.values.tolist())   # 二维列表
score_ls = [score.columns.tolist()]+score.values.tolist()
print(score_ls)
[['姓名', '学号', 'C语言', 'Java', 'Python', 'VB', 'C++', '总分'], 
 ['朱佳', 121701100511, '75.2', 93, 66, '85', 88, 407], 
 ['李思', 121701100513, '86', 76, 96, '93', 67, 418], 
 ['郑君', 121701100514, ' ', 98, 76, ' ', 89, 263], 
 ['王雪', 121701100515, '99', 96, 91, '88', 86, 460], 
 ['罗明', 121701100510, '95', 96, 85, '63', 91, 430]]
                    
