簡體   English   中英

python在沒有readlines命令的情況下更新一行

[英]python updating a line without readlines command

作為家庭作業的一部分,任務之一是更新特定的行,但是我們不允許將整個文件加載到內存中,一次只能加載1行

問題是,它將所有信息寫入文件的末尾,而不是替換同一行。

我嘗試使用搜索,但不斷出錯,這是我最接近它的某種方式。

我想做的就是:

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

這將保留所有行,只需將所有行中的PKR更改為123。 但保持相同的順序。

希望有人能告訴我我的錯誤是什么。 謝謝!

# 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)

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

findIndex函數

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

要讀取的txt示例

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

據我所知,如果只限於一個文件,那是不可能的。 看看這個答案,它解釋了不同的文件訪問方法: 被python文件模式“ w +”所混淆

如果允許使用兩個文件,那么一切就順其自然了,但是我不明白如何在不附加文件的情​​況下寫入現有文件; 即在文件末尾。

*您可以使用臨時文件並刪除原始文件,然后將臨時文件重命名為原始文件。 :) *

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM