0

as part as my homework one of the tasks is to update a specific line, but we are not allowed to load the whole file into the memory, but only 1 line at a time

the problem is, it writes all the info to the end of file instead of replace the same line.

i tried using seek, but kept getting errors, this is the closest i got it to somehow working.

what i want to do is take for example:

heap.update('currency', 'PKR', '123')

that will keep all the lines and just change PKR into 123 in all lines. but stay the same order.

hope someone can tell me what is my mistake. thanks!

# update a value in heap
def update(self, col_name, old_value, new_value, ):
    if self.is_empty() == 0: #check if file is empty
        return
    i = self.findIndex(col_name) #find what column im comparing to
    if i == -1: #if column out of bound
        return
    with open(self.name, "r") as f: #open file for read mode
        while True: 
            temp=''
            line = f.readline() #read line
            if line == '': #if line is empty, we are done
                break
            #split line into the 4 columns
            keep1 = line.split(',', 3)[0]; 
            keep2 = line.split(',', 3)[1];
            keep3 = line.split(',', 3)[2];
            keep4 = line.split(',', 3)[3];
            x = line.split(',')[i];
            if (x == old_value): #compare value and check if its equle, if it is:
                #create a new string with value
                if i == 0:
                    temp = new_value + "," + keep2 + "," + keep3 + "," + keep4;
                elif i == 1:
                    temp = keep1 + "," + new_value + "," + keep3 + "," + keep4;
                elif i == 2:
                    temp = keep1 + "," + keep2 + "," + new_value + "," + keep4;
                elif i == 3:
                    temp = keep1 + "," + keep2 + "," + keep3 + "," + new_value;
            with open(self.name, "a") as ww: #write value into file
                ww.write(temp)

self

def __init__(self, file_name):
    self.name = file_name;
    f = open(self.name, "w");
    f.close();

findIndex function

def findIndex(self, col_name):
    if self.is_empty() == 0:
        return -1
    elif col_name == 'lid':
        return 0
    elif col_name == 'loan_amount':
        return 1
    elif col_name == "currency":
        return 2
    elif col_name == "sector":
        return 3

example of txt to read

lid,loan_amount,currency,sector
653051,300.0,PKR,Food
653053,575.0,PKR,Trns
653068,150.0,INR,Trns
653063,200.0,PKR,Arts
653084,400.0,PKR,Food
653067,200.0,INR,Agri
653078,400.0,PKR,Serv
653082,475.0,PKR,Manu
653048,625.0,PKR,Food
653060,200.0,PKR,Trns
653088,400.0,PKR,Sale
653089,400.0,PKR,Reta
653062,400.0,PKR,Clth
653075,225.0,INR,Agri
653054,300.0,PKR,Trns
653091,400.0,PKR,Reta
653052,875.0,PKR,Serv
653066,250.0,INR,Serv
653080,475.0,PKR,Serv
653065,250.0,PKR,Food
653055,350.0,PKR,Food
653050,575.0,PKR,Clth
653079,350.0,PKR,Arts
653061,250.0,PKR,Food
653074,250.0,INR,Agri
653069,250.0,INR,Cons
653056,475.0,PKR,Trns
653071,125.0,INR,Agri
653073,250.0,INR,Agri
653059,250.0,PKR,Clth
653087,400.0,PKR,Manu
653076,450.0,PKR,Reta
Itzik Dan
  • 27
  • 4
  • cant put code wrap.. def delete(self, col_name, value): i=self.findIndex(col_name) if i==-1: return heapfile = open(self.name, "r+"); while True: line=heapfile.readline() # for line in heapfile: if line=='': break x = line.split(',')[i]; if (x == value): heapfile.seek(0-len(line)-1,1) heapfile.write('#'); heapfile.seek(heapfile.tell()) heapfile.seek(0 + len(line), 1) heapfile.close(); – Itzik Dan May 19 '18 at 20:52

2 Answers2

0

As far as I can see, that is not possible if you are limited to having just one file. Check out this answer which explains the different file access methods: Confused by python file mode “w+”.

If you are allowed to use two files, well then it's plain sailing but I don't see how you can write to an existing file without it being append; i.e. at the end of the file.

figbeam
  • 7,001
  • 2
  • 12
  • 18
  • if you'r asking if i'm allowed to use a+ mode, then yes we are allowed. another thing is we know for sure the length of the updated line will be the same as the old line length (if that helps). i built a delete function that does work im adding it as well maybe it will give some ideas on how to make the update line since they should be similiar – Itzik Dan May 19 '18 at 20:52
  • 1
    Editing files in place is more than possible - check [this answer](https://stackoverflow.com/a/49734119/7553525) for a couple of ways to do it - but I already gave a link to the OP to see how its done. His main issue is in how he approaches the whole thing. – zwer May 21 '18 at 13:58
-1

*you can use a temp file and remove the original file and rename the temp file to the original. :) *

  • Welcome to StackOverflow, when you get a moment please read our [help] and take the [tour]. When you have enough reputation you will be able to comment on a question. As it stands, while your answer may provide a a solution, it's not really an answer to the question. – bendl May 21 '18 at 13:57