Python 自定义CSV解析器
从头开始读/写csv文件,而不是使用csv模块,这是一种糟糕的形式吗? 我有一个文件,其中每行表示文件操作的参数列表:Python 自定义CSV解析器,python,parsing,csv,python-3.x,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”和列表[值、值、值]之间进行转换 def get_csv(
"time", "old path", "new path", "metadata1", "metadata2" ...
"time", "old path", "new path", "metadata1", "metadata2" ...
我编写了一些方法,可以在字符串“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
每次我用列表更新提要时,它也会更新文件,而不必每次都重写整个文件
这样我就可以使用脚本对文件进行排序,并在每次操作后记录参数。比如:
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模块会有好处吗?使用csv模块的好处(除其他外,我确信)包括:
- 更多测试:其他人和许多开发人员正在使用此代码,并且(希望如此)报告和修复bug
- 您不必编写所有这些代码
在一般编程中,如果开放的、经过测试的、流行的和受支持的库已经满足了您的需要,为什么不省去您的工作,并将这些工作放到代码的其他部分呢?形式不错,但可能毫无意义。