python更新没有readlines命令的行

时间:2018-05-19 17:42:26

标签: python

作为我的作业的一部分,其中一项任务是更新特定的行,但我们不允许将整个文件加载到内存中,但一次只能加载一行

问题是,它将所有信息写入文件末尾,而不是替换同一行。

我尝试使用搜索,但不断收到错误,这是我最接近的工作方式。

我想做的就是举例:

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

2 个答案:

答案 0 :(得分:0)

据我所知,如果您只限制一个文件,则无法做到这一点。查看此答案,该答案解释了不同的文件访问方法:Confused by python file mode “w+”

如果您被允许使用两个文件,那么它一帆风顺,但我不知道如何在不附加文件的情​​况下写入现有文件;即在文件的末尾。

答案 1 :(得分:-1)

*您可以使用临时文件删除原始文件,并将临时文件重命名为原始文件。 :) *