![](/img/trans.png)
[英]How do I remove [] and '' when printing from a csv file python 3
[英]How do I put students names from a CSV file into a List when printing
打印時,我正在嘗試這樣做,以便我可以將 CSV 中的學生姓名和詳細信息及其計算出的 UCAS 分數放入新的 CSV 文件中,但我無法將其打印出來。
例如,我試圖把這個:
我如何將信息放在 Forename、Surname 和 Ucas Points 內,而不是在下面打印。
Grades.csv 文件中的一個示例:
名字,姓氏,等級
比爾,史密斯,P,P,P,M,P,D,D,P,M,P
with open('Grades.csv', 'r') as f:
data = f.readlines()
with open('Results.csv', 'w') as f:
fieldnames = ['Forename', 'Surname', 'Ucas Points']
csv_writer = csv.writer(f)
information = ['Forename: ','Surname: ','Ucas Points: ']
students = []
for studentLine in data[1::]:
students.append(studentLine.replace("\n", "").split(","))
for student in students:
line_count = 0
ucas = 0
for index in student[4::]:
if line_count == 7:
if index == "P":
ucas += 9 * 1.066
elif index == "M":
ucas += 9 * 2.133
else:
ucas += 9 * 3.2
else:
if index == "P":
ucas += 6 * 1.066
elif index == "M":
ucas += 6 * 2.133
else:
ucas += 6 * 3.2
line_count += 1
print(student[0] + " " + student[1] + " " + str(ucas))
print(information)
您可以嘗試下面的代碼,它應該會有所幫助。
with open('Grades.csv', 'r') as f:
data = f.readlines()
with open('Results.csv', 'w') as f:
fieldnames = ['Forename', 'Surname', 'Ucas Points']
csv_writer = csv.writer(f)
information = ['Forename: ','Surname: ','Ucas Points: ']
students = []
res = []
for studentLine in data[1::]:
students.append(studentLine.replace("\n", "").split(","))
for student in students:
cur_dic = {}
line_count = 0
ucas = 0
for index in student[4::]:
if line_count == 7:
if index == "P":
ucas += 9 * 1.066
elif index == "M":
ucas += 9 * 2.133
else:
ucas += 9 * 3.2
else:
if index == "P":
ucas += 6 * 1.066
elif index == "M":
ucas += 6 * 2.133
else:
ucas += 6 * 3.2
line_count += 1
cur_dic['Forename'] = student[0]
cur_dic['Surname'] = student[1]
cur_dic['Ucas Points'] = str(ucas)
res.append(cur_dic) # in case you want to use all the data at once
print(cur_dic)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.