更新时间:2023-09-13 17:25:34
实际上,即使这些新列的数据都存储在单个列表中,您实际上也试图将几列追加到现有文件中.***以不同的方式在ind_lst
中安排数据.但是由于您尚未展示如何完成此操作,因此下面的代码可与您问题中的格式配合使用.
You're actually trying to append several columns to the existing file, even if the data for these new columns is all stored in a single list. It might be better to arrange the data in the ind_lst
differently. but since you haven't showed how that's done, the code below works with the format in your question.
由于修改CSV文件非常麻烦(因为它们实际上只是文本文件),因此简单得多简单地使用合并后的数据创建一个新文件,然后对该文件进行重命名以匹配 删除原始文件 之后的原始文件(现在已被警告).
Since modifying CSV files is tricky—since they're really just text file—it would be much easier to simply create a new file with the merged data, and then rename that file to match the original after deleting the original (you've now been warned).
import csv
from itertools import izip # Python 2
import os
import tempfile
master_lst = [
'read',
'ACACCUGGGCUCUCCGGGUACC',
'ACGGCUACCUUCACUGCCACCC',
'AGGCAGUGUGGUUAGCUGGUUG'
]
ind_lst = [
'sample1',
'3',
'3',
'1',
'sample2',
'4',
'4',
'1'
]
csv_filename = 'output.csv'
def grouper(n, iterable):
's -> (s0,s1,...sn-1), (sn,sn+1,...s2n-1), (s2n,s2n+1,...s3n-1), ...'
return izip(*[iter(iterable)]*n)
# first create file to update
with open(csv_filename, 'wb') as f:
writer = csv.writer(f)
writer.writerows(((row,) for row in master_lst))
# Rearrange ind_lst so it's a list of pairs of values.
# The number of resulting pairs should be equal to length of the master_lst.
# Result for example data: [('sample1', 'sample2'), ('3', '4'), ('3', '4'), ('1', '1')]
new_cols = (zip(*grouper(len(master_lst), ind_lst)))
assert len(new_cols) == len(master_lst)
with open(csv_filename, 'rb') as fin, tempfile.NamedTemporaryFile('r+b') as temp_file:
reader = csv.reader(fin)
writer = csv.writer(temp_file)
nc = iter(new_cols)
for row in reader:
row.extend(next(nc)) # add new columns to each row
writer.writerow(row)
else: # for loop completed, replace original file with temp file
fin.close()
os.remove(csv_filename)
temp_file.flush() # flush the internal file buffer
os.fsync(temp_file.fileno()) # force writing of all data in temp file to disk
os.rename(temp_file.name, csv_filename)
print('done')
创建后文件的内容,然后进行更新:
Contents of file after creation followed by update:
read,sample1,sample2
ACACCUGGGCUCUCCGGGUACC,3,4
ACGGCUACCUUCACUGCCACCC,3,4
AGGCAGUGUGGUUAGCUGGUUG,1,1