I got stuck in getting the right result from a simple piece of Python code ( I am a Python beginner anyway).
Given a csv input file (ListInput.csv):
pKT, pET, pUT,
and another csv file which contains features of many of these elements (Table.csv):
pBR,156,AATGGT,673,HHHTTTT,
pUT,54,CCATGTACCTAT,187,PRPTP,
pHTM,164,GGTATAG,971,WYT,
pKT,12,GCATACAGGAC,349,,
pET,87,GTGACGGTA,506,PPMK,
............ and so on
I aim to get a selection based on the first csv file elements in order to get a csv file as output (WorkingList.txt), in this case the expected result would be:
pKT,12,GCATACAGGAC,349,,
pET,87,GTGACGGTA,506,PPMK,
pUT,54,CCATGTACCTAT,187,PRPTP,
I wrote the following script which does not gives errors but end up with an empty file as output. I am tryng to understand why since a couple of days with no success. Any help is gratly appreciated.
#!/usr/bin/python
import csv
v = open('ListInput.csv', 'rt')
csv_v = csv.reader(v)
vt = open('Table.csv', 'rt')
csv_vt = csv.reader(vt)
with open("WorkingList.txt", "a+t") as myfile:
pass
for el in csv_v:
for var in csv_vt:
if el == var[0]:
myfile.write(var)
myfile.close()
See Question&Answers more detail:
os