自定义CSV解析器

-1 投票
1 回答
506 浏览
提问于 2025-04-18 01:05

直接从头开始读写CSV文件,而不使用CSV模块,这样做会不好吗?

我有一个文件,每一行都代表一个文件操作的参数列表:

"time", "old path", "new path", "metadata1", "metadata2" ...
"time", "old path", "new path", "metadata1", "metadata2" ...

我写了一些方法,可以把类似 '"值", "值", "值"' 的字符串和 [值, 值, 值] 的列表互相转换。

def get_csv(iterable):
    "Return iterable as a string of comma-separated-values"
    return ', '.join('"{}"'.format(str(i)) for i in iterable)

def get_list(string):
    "Return a list from a string of comma-separated-values"
    # remove first/last parantheses and separate by ", "
    return string[1:-1].split('", "')

然后我把这些方法放在一个类里。

class LogParser:
    def __init__(self):
        self.feed = []

    def read(self, fp):
        "Parse log feed from a csv file"
        with open(fp, 'r') as csvfile:
            for line in csvfile:
                self.feed.append(get_list(line.strip()))

    def write(self, fp):
        "Write log feed to a csv file"
        with open(fp, 'w') as csvfile:
            for entry in self.feed:
                csvfile.write(get_csv(entry) + '\n')

    def update(self, entry, fp):
        "Update entry to feed and append entry to a csv file, on a new line"
        with open(fp, 'a') as csvfile:
            csvfile.write(get_csv(entry) + '\n')
        self.feed.append(entry)

    def search(self, query):
        ## return any lines in self.feed that match the query
        pass

每次我用一个列表更新数据时,它也会更新文件,而不需要每次都重写整个文件。

这样我就可以用脚本来排序文件,并在每次操作后记录参数。就像这样:

logger = LogParser()

def sortfiles(sourcedir):
    for filename in os.listdir(sourcedir):
        old_path = os.path.join(sourcedir, filename)
        metadata1 = # something used to sort the file
        metadata2 = # something else used to sort the file
        new_path = # determine the new path using metadata
        time = # get the time

        try:
            os.rename(old_path, new_path)
        except SomeError:
            raise('Something went wrong when trying to move the file!') 
        else:
            logger.update([time, old_path, new_path, metadata1, metadata2])

这个方法可以用,但这样做有什么问题吗?如果我使用CSV模块,会有什么好处呢?

1 个回答

2

使用csv模块的好处(当然还有其他好处)包括:

  • 更多的测试:其他人和很多开发者都在使用这段代码,并且(希望)会报告和修复其中的错误。
  • 你不需要自己写这么多代码。

在编程中,如果已经有开源、经过测试、受欢迎并且有支持的库可以满足你的需求,那为什么不省点力气,把精力放在代码的其他部分呢?

撰写回答