Home > Net >  Why do I keep getting random "\n" at the last dictionary value
Why do I keep getting random "\n" at the last dictionary value

Time:02-03

JP.txt:

Template: ALE
Student's name: JP
Grades = {'Mathématiques': '18.0', 'Français': '18.0', 'ALE': '18.0', 'Espagnole': '18.0', 'Anglais': '18.0', 'SVT': '18', 'SPC': '18', 'Technologie': '18.0', 'HGPF': '18.0', 'EMC': '18.0', 'EPS': '18.0', 'Art': '18.0'}
Coefs = {'Mathématiques': '4', 'Français': '4', 'ALE': '1', 'Espagnole': '1', 'Anglais': '2', 'SVT': '2', 'SPC': '2', 'Technologie': '1', 'HGPF': '2', 'EMC': '1', 'EPS': '1', 'Art': '18'}
Student's overall average: 18.0

and here's my code

def removeline(filename, a):
    a_file = open(filename, "r")

    lines = a_file.readlines()
    a_file.close()

    new_file = open(filename, "w")
    for line in lines:
        if line.strip("\n") != a:
            new_file.write(line)

    new_file.close()

import os
file_path = 'D:/Users/Jean Paul/OneDrive/Programming/Programs/Prog 2 - Moyenne de Notes/saved_grades/'
Complete_name = os.path.join(file_path   '.saved_list'   '.dat')

Complete_name = file_path   "JP.txt"
selected_saved = open(Complete_name, "r")
saved_file_data = selected_saved.read()
saved_file_data = saved_file_data.replace("\'", "").replace("{", "").replace("}", "").replace(" =", ":").replace("Coefs", "Coefficients").replace("h�", "hé").replace("n�", "nç")#.replace("\\\\\\\\n\\\\n\\n\n", "\n")
print(saved_file_data)

selected_saved = open(Complete_name, "r")
content = selected_saved.readlines()
template = content[0][10:]
studentname = content[1][16:]
grades = content[2][9:].replace("{", "").replace("}", "").replace("\'", "").replace("h�", "hé").replace("n�", "nç").replace(": ", "=").replace(" ", "")
grades = dict(subString.split("=") for subString in grades.split(","))
coef = content[3][8:].replace("{", "").replace("}", "").replace("\'", "").replace("h�", "hé").replace("n�", "nç").replace(": ", "=").replace(" ", "")
coef = dict(subString.split("=") for subString in coef.split(","))
average = content[4][27:]

print("Which of your grades would you like to edit? Please enter the full material name and separate each one with a space only (\" \"). Use spaces ONLY to separate material names.")
materials = str(input())
while "," or "  " in materials:
    if "," in materials:
        materials = materials.replace(",", " ")
    if "  " in materials:
        materials = materials.replace("  ", " ")
        continue
    if "," and "  " not in materials:
        break

materials_list = materials.split()
print("You will be editing your grades of: "   materials.replace(" ", ", "))
number_of_words = len(materials_list)
r = 0
a = 0
while r < number_of_words:
    if materials_list[a] in grades.keys():
        r = r   1
        a = a   1
        continue
    else:
        fixed_typo = str(input(materials_list[a]   " doesn't match any of your materials, please enter the right material name: "))
        materials_list[a] = fixed_typo

if number_of_words > 1:
    print("")
    r = 0
    a = 0
    while r < number_of_words:
        print("Your old grade in "   materials_list[a]   " is: "   grades[materials_list[a]])
        new_grade = input("How much do you want it to become? ")
        if 0 > float(new_grade) or float(new_grade) > 20:
            print("Enter a number between 0 and 20.")
            continue
        grades[materials_list[a]] = new_grade
        r = r   1
        a = a   1
        print("")

selected_saved.close()
selected_saved = open(Complete_name, "w")
selected_saved.truncate()
selected_saved.close()
selected_saved = open(Complete_name, "a")
print('Template: '   template, file = selected_saved)
print('Student\'s name: '   studentname, file = selected_saved)
print('Grades = '   str(grades), file = selected_saved)
print('Coefs = '   str(coef), file = selected_saved)
print('Student\'s overall average: '   str(average), file = selected_saved)
selected_saved.close()
removeline(Complete_name, "")

In this program I am getting the data of another file and asking the user if he wants to edit some of his grades that are in this file. If he choose to edit "SVT" and "SPC" he'll get the grades of theses materials and then will be able to change them. After being changed the file (JP.txt) gets cleared and then everything is reprinted to it. But after being reprinted I am finding these weird stuff (\n) at the end of each dictionary (Grades and Coefs). I keep having more \n adding at same place every time I rerun the program.

Here's JP.txt after:

Template: ALE
Student's name: JP
Grades = {'Mathématiques': '18.0', 'Français': '18.0', 'ALE': '18.0', 'Espagnole': '18.0', 'Anglais': '18.0', 'SVT': '18', 'SPC': '18', 'Technologie': '18.0', 'HGPF': '18.0', 'EMC': '18.0', 'EPS': '18.0', 'Art': '18.0\n'}
Coefs = {'Mathématiques': '4', 'Français': '4', 'ALE': '1', 'Espagnole': '1', 'Anglais': '2', 'SVT': '2', 'SPC': '2', 'Technologie': '1', 'HGPF': '2', 'EMC': '1', 'EPS': '1', 'Art': '18\n'}
Student's overall average: 18.0

I didn't add the code that recalculates the average yet, because I am not working on that now.

CodePudding user response:

When you are slicing the content for grades and coef then it was adding \n at the end of line . Try replacing

grades = content[2][9:]
coef = content[3][8:]

with

grades = content[2][9:-1]
coef = content[3][8:-1]

CodePudding user response:

This answer resolved my problem and it is from @Håken Lid

Presumably this happens if the string grades ends with a \n here : dict(subString.split("=") for subString in grades.split(",")). Try grades = grades.strip() first to remove trailing whitespace.

  •  Tags:  
  • Related