How to write a python program for the following?
0
0
Entering edit mode
2.6 years ago
arr234 ▴ 40

I have 50 csv files named with a unique id in a folder. In each of the csv files, there are names and their corresponding scores (out of 100). Now I have to filter the csv sheet based on the score>50 and get the corresponding names in text files named with the unique id just as the csv file from which it was taken and put them in a new folder. The following is the code I have written but the output is not as expected.

import glob, os
os.chdir("path of the folder")
for file in glob.glob("*.csv"):
    with open(file,'r') as fin:
        next(fin) #skip the header row
        for row in csv.reader(fin, delimiter=' '):
            for col in csv.reader(row, delimiter=','):
                if int(col[1])>50
                    for i, line in enumerate(fin):
                        f=open("Path to the new folder/output_%i.txt" %i, 'w')
                        f.write(col[0])
                        f.close()

The above code generated one file for each score which lies greater than 50 which i dont want. So I request you to correct the above code to help me get the required output. Thank you in advance.

python • 670 views
ADD COMMENT
0
Entering edit mode

How is this a bioinformatics question? As presented this is purely a python programming question and should probably be asked elsewhere such as StackOverflow.

ADD REPLY
0
Entering edit mode

You are right. It is not bioinformatics question. I have similar bioinformatics data which I am not suppose to disclose. Hence I have replaced it with different terms.

ADD REPLY

Login before adding your answer.

Traffic: 1388 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6