所以我有一个csv,我试图通过插入行来修改它,但也要在关闭csv之前循环执行迭代,将其他列附加到csv。我尝试了各种不同的方法,但似乎一旦完成了writerows
函数,下一步就不会处理任何writerows
。有没有办法做到这一点,或者我必须保存writerows
结果,重新打开CSV文件,然后完成附加?我已经将rows
插入到CSV中,然后注释掉output.writerows(rows)
,for
循环在这种情况下确实有效。但是,插入了writerows
后,它就不再像我希望的那样工作了,因为它跳过了函数中编写的任何内容。
以下是我一直在使用的一些示例代码:
import csv
import itertools
import uuid
from datetime import datetime
rows = [[1000000, 'Test', 'Test', 179, 1111, 222, 5, 'Test', 'Test', 1111, 'Test', 222, 'Test', 1111, 22222, 'Test', '', 1, 2],
[1000000, 'Test', 'Test', 179, 1111, 222, 5, 'Test', 'Test', 1111, 'Test', 222, 'Test', 1111, 22222, 'Test', '', 1, 2],
[1000000, 'Test', 'Test', 179, 1111, 222, 5, 'Test', 'Test', 1111, 'Test', 222, 'Test', 1111, 22222, 'Test', '', 1, 2],
[1000000, 'Test', 'Test', 179, 1111, 222, 5, 'Test', 'Test', 1111, 'Test', 222, 'Test', 1111, 22222, 'Test', '', 1, 2],
[1000000, 'Test', 'Test', 179, 1111, 222, 5, 'Test', 'Test', 1111, 'Test', 222, 'Test', 1111, 22222, 'Test', '', 1, 2],
[1000000, 'Test', 'Test', 179, 1111, 222, 5, 'Test', 'Test', 1111, 'Test', 222, 'Test', 1111, 22222, 'Test', '', 1, 2],
[1000000, 'Test', 'Test', 179, 1111, 222, 5, 'Test', 'Test', 1111, 'Test', 222, 'Test', 1111, 22222, 'Test', '', 1, 2],
[1000000, 'Test', 'Test', 179, 1111, 222, 5, 'Test', 'Test', 1111, 'Test', 222, 'Test', 1111, 22222, 'Test', '', 1, 2],
[1000000, 'Test', 'Test', 179, 1111, 222, 5, 'Test', 'Test', 1111, 'Test', 222, 'Test', 1111, 22222, 'Test', '', 1, 2]]
def _sanitize_report(source_file, dest_file, append_timestamp, timestamp_format, append_filename, file,
append_id):
with open(source_file, 'r') as report:
reader = csv.reader(report)
with open(dest_file, 'w') as output_file:
data_rows = 0
output = csv.writer(output_file)
output.writerows(rows)
for index, row in enumerate(reader):
if append_timestamp and append_filename and append_id:
output.writerow(itertools.chain([datetime.today().strftime(timestamp_format)],
[str(file).replace(file[0:3], '')],
[uuid.uuid4()]))
elif append_timestamp and append_filename:
output.writerow(itertools.chain([datetime.today().strftime(timestamp_format)],
[str(file).replace(file[0:3], '')]))
elif append_timestamp and append_id:
output.writerow(itertools.chain([datetime.today().strftime(timestamp_format)], [uuid.uuid4()]))
elif append_filename and append_id:
output.writerow(itertools.chain([str(file).replace(file[0:3], '')], [uuid.uuid4()]))
elif append_timestamp:
output.writerow(itertools.chain( [datetime.today().strftime(timestamp_format)]))
elif append_filename:
output.writerow(itertools.chain([str(file).replace(file[0:3], '')]))
elif append_id:
output.writerow(itertools.chain([uuid.uuid4()]))
else:
output.writerow([value.replace('(not set)', '') for value in row])
data_rows += 1
_sanitize_report('test.csv', 'test_' + str(uuid.uuid4()) + '.csv',
append_timestamp=True, timestamp_format=datetime.utcnow().isoformat() + 'Z',
append_filename=True, file='123Test_success', append_id=True)
我正在寻找的最终结果是,用户可以决定需要附加到结果中的内容,并将文件保存到一个新位置,其中包含所需的所有内容。这可以在单个函数中实现吗?还是必须将其分解?我尝试了多种不同的方法,但都没有成功,我已经尝试了大约一周了。
如有任何帮助,我们将不胜感激!
当您调用writerows((,然后对writerow((进行一些单独的调用时,会发生什么?
rows = [
["r1c1", "r1c2", "r1c3"],
["r2c1", "r2c2", "r2c3"],
["r3c1", "r3c2", "r3c3"],
]
with open("output.csv", "w", newline="") as f_out:
writer = csv.writer(f_out)
writer.writerows(rows)
writer.writerow(["r4c1", "r4c2", "r4c3"])
writer.writerow(["r5c1", "r5c2", "r5c3"])
发生这种情况:
r1c1,r1c2,r1c3
r2c1,r2c2,r2c3
r3c1,r3c2,r3c3
r4c1,r4c2,r4c3
r5c1,r5c2,r5c3
如果在调用writerows((时单个行已经在列表中,则会发生相同的情况,或者如果迭代调用writerow((的行列表,则会出现相同的情况。
writerow((写入一行,writerows((写入行列表。
如果您想选择性地将某些列添加到数据中,则需要在调用writerow((或writerows((之前将这些列添加到行中。以下是writerow((的外观:
...
ts = datetime.today()
file_id = "foo_1"
for row in rows:
if append_timestamp:
row.append(ts)
if append_id:
row.append(file_id)
...
writer.writerow(row)