I already have a CSV file created from a list using CSV writer. I want to append another list created through a for loop columnwise to a CSV file.
The first code to create a CSV file is as follows:
with open("output.csv", "wb") as f:
writer = csv.writer(f)
for row in zip(master_lst):
writer.writerow(row)
I created the CSV file using the list master_lst
and the output is as follows:
read
ACACCUGGGCUCUCCGGGUACC
ACGGCUACCUUCACUGCCACCC
AGGCAGUGUGGUUAGCUGGUUG
Then I create another list (ind_lst
) through a for
loop and the contents of the list has to be appended columnwise to the CSV file created in the previous step. I used the following code:
with open("output.csv", "ab") as f:
writer = csv.writer(f)
for row in zip(ind_lst):
writer.writerow(row)
The output I obtained is as follows:
read
ACACCUGGGCUCUCCGGGUACC
ACGGCUACCUUCACUGCCACCC
AGGCAGUGUGGUUAGCUGGUUG
sample1
3
3
1
sample2
4
4
1
However I need the output columnwise as follows:
read sample1 sample2
ACACCUGGGCUCUCCGGGUACC 3 4
ACGGCUACCUUCACUGCCACCC 3 4
AGGCAGUGUGGUUAGCUGGUUG 1 1
I checked for solutions but I can find only solutions for appending row wise, but I need to append it columnwise: append new row to old csv file python
I used writer.writerows
instead of writer.writerow
but I get this error:
_csv.Error: sequence expected
The output is as follow:
read
ACACCUGGGCUCUCCGGGUACC
ACGGCUACCUUCACUGCCACCC
AGGCAGUGUGGUUAGCUGGUUG
s a m p l e 1
As you can see, it prints the first element of the list in each cell and terminates thereafter with an error. I am a beginner in python, so if anyone could help solve this issue that would be awesome.
EDIT:
The master_lst is created using the following code:
infile= open(sys.argv[1], "r")
lines = infile.readlines()[1:]
master_lst = ["read"]
for line in lines:
line= line.strip().split(',')
fourth_field = line [3]
master_lst.append(fourth_field)
the ind_lst is created using the following code:
for file in files:
ind_lst = []
if file.endswith('.fa'):
first = file.split(".")
first_field = first [0]
ind_lst.append(first_field)
fasta= open(file)
individual_dict= {}
for line in fasta:
line= line.strip()
if line == '':
continue
if line.startswith('>'):
header = line.lstrip('>')
individual_dict[header]= ''
else:
individual_dict[header] += line
for i in master_lst[1:]:
a = 0
if key in individual_dict.keys():
a = individual_dict[key]
else:
a = 0
ind_lst.append(a)