自定义CSV解析器

时间:2014-04-02 22:51:30

标签: python parsing csv python-3.x

从头开始读取/写入csv文件而不是使用csv模块会不好?

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

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

我写了一些让我在'"value", "value", "value"'字符串和[value, value, value]

列表之间进行转换的方法
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

每次使用列表更新Feed时,它都会更新文件,而不必每次都重写整个文件。

这样我就可以使用脚本对文件进行排序,并在每次操作后记录参数。类似的东西:

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 个答案:

答案 0 :(得分:2)

使用csv模块的好处(除此之外,我确定)是:

  • 更多测试:其他人和许多开发人员正在使用此代码,并且(希望如此)报告和修复错误。
  • 您不必编写所有此代码

在编程中,如果开放,测试,流行和支持的库已经满足您的需求,为什么不节省您的精力并将这些精力投入代码的其他部分呢?