gpt4 book ai didi

python - 如果没有数据,不要用 csv DictWriter 写入文件

转载 作者:太空宇宙 更新时间:2023-11-04 00:36:25 24 4
gpt4 key购买 nike

我有一个 Python (3.4) 例程,它使用生成器编写 csv 文件。但是,根据参数集,可能没有任何数据,在这种情况下,我不想写入 csv 文件。 (它只会写入仅包含标题的文件)。

现在,创可贴是在生成后计算行数,然后删除文件,但肯定有更好的方法,同时保留让生成器成为唯一知道是否有数据的代码的模式给定的参数,(也不必两次调用生成器):

def write_csv(csv_filename, fieldnames, generator, from_date, to_date, client=None):
with open(csv_filename, 'w', newline='') as csv_file:
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames, delimiter='\t')
csv_writer.writeheader()
csv_writer.writerows(generator(from_date, to_date, client))

# If no rows were written delete the file, we don't want it
with open(csv_filename) as f:
lines = sum(1 for _ in f)
if lines == 1:
f.close()
os.remove(f.name)


def per_client_items_generator(from_date, to_date, client):
return (per_client_detail(client, sales_item) for sales_item in
sales_by_client.get(client))

最佳答案

您可以使用 itertools 查看第一项,然后将其放回生成器中:

import itertools
gen = generator(from_date, to_date, client)
try:
# try to get an element
first = next(gen)
except StopIteration:
pass
else:
# run this if there was no exception:
gen = itertools.chain([first], gen)
csv_writer.writeheader()
csv_writer.writerows(gen)

这有点短,但可能更难阅读:

import itertools
gen = generator(from_date, to_date, client)
try:
# pop an element then chain it back in
gen = itertools.chain([next(gen)], gen)
except StopIteration:
pass
else:
# run this if there was no exception:
csv_writer.writeheader()
csv_writer.writerows(gen)

或者这不使用可见的 try/catch 代码(尽管 next() 中可能有相同数量的代码):

import itertools
sentinel = object() # special flag that couldn't come from the generator
gen = generator(from_date, to_date, client)

# try to get something
first = next(gen, sentinel)
if first is not sentinel:
# got a meaningful item, put it back in the generator
gen = itertools.chain([first], gen)
csv_writer.writeheader()
csv_writer.writerows(gen)

(这些灵感来自 Stephen Rauch 的回答,但有一些调整。)

关于python - 如果没有数据,不要用 csv DictWriter 写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43849040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com