简体   繁体   English

python在没有readlines命令的情况下更新一行

[英]python updating a line without readlines command

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 作为家庭作业的一部分,任务之一是更新特定的行,但是我们不允许将整个文件加载到内存中,一次只能加载1行

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. 这将保留所有行,只需将所有行中的PKR更改为123。 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 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

example of txt to read 要读取的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

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+” . 看看这个答案,它解释了不同的文件访问方法: 被python文件模式“ 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; 如果允许使用两个文件,那么一切就顺其自然了,但是我不明白如何在不附加文件的情​​况下写入现有文件; ie at the end of the file. 即在文件末尾。

*you can use a temp file and remove the original file and rename the temp file to the original. *您可以使用临时文件并删除原始文件,然后将临时文件重命名为原始文件。 :) * :) *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM