Modify and Search

Hello. I want to modify and sort but it’s only modifying why is it not searching?

std_file = open("students.txt", "r") #reading the contents in students.txt file
std_records = std_file.readlines() #readlines() return all lines in the file, as a list where each line is an item in the list object

record = 0
for std_record in std_records:
   print("[",(record+1),"]",std_record.strip()) #printing each line from text file

record_num=int(input("\nEnter record number you wanted to edit: "))
std_details = std_records[record_num-1].split(",") #value of selected record from text file transfer to std_details list variable

record = 0
for std_record in new_list:
   print("[",(record+1),"]",std_record.strip()) #printing each line from text file

element = 0
for record_element in std_details:
   print("[",(element+1),"]",record_element) #printing the selected record details

element_number=int(input("Enter element number you wanted to edit: "))

print("\nCurrent element value:",std_details[element_number-1])

new_element_value = input("Enter new element value: ")


if element_number==4 and record!=record_num:
   new_line="\n" #to be added when updating the record in text file, so each record separated by new line

std_details[element_number-1] = new_element_value+new_line #assigning new value into selected element

updated_record = ','.join(std_details) #join() method takes all items in a list and joins them into one string. updated_record is a string

std_records[record_num-1] = updated_record #now selected line has updated value

new_list = []

while std_records:
   minimum = std_records[0]
   for x in std_records:
      if x < minimum:
         minimum = x


with open("students.txt", "w") as file:
    file.writelines(std_records) #writing the updated student records into text file

print("\nRecord updated!")


Hi, it’s hard to help without knowing what exactly is wrong. Like, exactly what output do you get, and what would you expect?

Also the code is hard to read like this, please enclose it in a markdown code block so indentation is preserved, like this:

if a == b:

The py after ``` is optional, it configures Python syntax highlighting. The result should look similar to this:

if a == b:

hello, thank you for the respond.
the output i get is the record of text file and im able to modify it. the output i want is i get the record it should be sorting in ascending order and im able to modify

I’m afraid that’s not enough detail. To help we’d need to know exactly (as in, actual content!) how your input file looks, what output you get, and what you want to get.

Also, please edit your first post to use a code block as I described above. Otherwise the code is unreadable.

1 Like

hello, i have edit it thank you. and that’s how my text file looks like

I think the main issue is that when writing out the results you use std_records, not new_list, which seems to be supposed to hold the sorted data.

That said, I think the code above is not exactly what you use, because new_list is used before being assigned to, which should crash with a NameError.

1 Like

thank you so much!! i didn’t notice it.

1 Like