Python按照值对文件进行排序,无需加载到s。

2024-06-17 10:38:34 发布

您现在位置:Python中文网/ 问答频道 /正文

作为我家庭作业的一部分。我们假设按照一列对文件进行排序,不允许使用list,也不允许将整个文件加载到堆栈中,但一次只能加载一行。 我们可以创建新文件并在以后删除它们

问题出在“create”函数中。我想做的是从“source\u file”中逐行提取,然后将它们放入我的“self”文件中,但当我将它们输入“self”时,我希望它们是有序的。你知道吗

但由于某种原因,我在文本文件中收到奇怪的字母。。。 希望有人能帮我,谢谢。你知道吗

这是我运行的函数

sf = SortedFile('SortedFile.txt', 'currency')
sf.create('kiva.txt')

这是kiva 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
653072,250.0,INR,Agri
653086,400.0,PKR,Food
653058,400.0,PKR,Serv
653083,475.0,PKR,Serv
653057,350.0,PKR,Reta
653090,475.0,PKR,Reta
653064,250.0,PKR,Heal
653077,475.0,PKR,Food
653081,200.0,PKR,Educ

这是密码

def __init__(self, file_name, col_name):
    self.name = file_name # create new file
    self.sort = col_name  # get name of col to compare
    f = open(self.name, "w+")  # create the file on pc
    f.close()  # close file
    """
    :param file_name: the name of the sorted file to create. example: kiva_sorted.txt
    :param col_name: the name of the column to sort by. example: 'lid'
    """

# return if heap is empty
def is_empty(self):
    heapfile = open(self.name, "r")  # open file in read mode
    if heapfile.readline() == '':  # if empty
        heapfile.close()  # close and return 0
        return 0
    else:
        heapfile.close()  # close and return 1 if not empty
        return 1
    """
    :return if the heap is empty or not (depanding if size is 0)
    """

# find what column we are in
def findIndex(self, col_name):
    if self.is_empty() == 0:  # check if col doesnt exist
        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

def create(self, source_file):
    f = open(source_file, "r")
    heapfile = open(self.name, "w+")
    i = self.findIndex(self.sort)  # get what column we want to compare
    while True:
        line = f.readline()  # read sourcefile line
        if line == '':  # if line is empty, we are done
            break
        if self.is_empty() == 0: # if file is empty
            heapfile.write(line) # write the 1st line
            line = f.readline() # read next line
        sourceCompare = line.split(',')[i]  # get compare value of source
        for lineSort in heapfile: # now go over new file, and start puting them inside file in order
            if line == '':  # if line is empty, we are done
                heapfile.write(lineSort)
                break
            newFileCompare = lineSort.split(',')[i]  # get compare value of heap
            if newFileCompare > sourceCompare:
                heapfile.write(line)
            # heapfile.write(lineSort)
    f.close()
    heapfile.close()
    """
    The function create sorted file from source file.
    :param source_file: the name of file to create from. example: kiva.txt
    """

Tags: thenameselfreturniffoodiscreate
1条回答
网友
1楼 · 发布于 2024-06-17 10:38:34

我认为错误在于:

           if newFileCompare > sourceCompare:
            heapfile.write(line)

您希望行将插入到正确的位置,但它总是附加到末尾。你知道吗

另一个问题可能是,每次迭代heapfile时,都不会返回到它的顶部。如果需要,可以使用seek(0)返回文件顶部。你知道吗

相关问题 更多 >