简体   繁体   中英

How do I put students names from a CSV file into a List when printing

When printing I am trying to make it so I can put the Student Names and Details from a CSV with their calculated UCAS score into a new CSV file but I am having trouble printing this out.

For example I am trying to turn this:

  • Jonah Whale 63.96900000000001 ['Forename: ','Surname: ','Ucas Points: ']
  • Into this: ['Forename: Jonah ','Surname: Whale ','Ucas Points:63.96900000000001 ']

How would I get the information to sit inside of the Forename, Surname and Ucas Points instead of printing it underneath.

An example from the Grades.csv file:

Forename,Surname, Grades

Bill,Smith,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)

You can try the below code it should help.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM