2

I used the csv module to create lists from a data file. It looks something like this now:

['unitig_5\t.\tregion\t401\t500\t0.00\t+\t.\tcov2=3.000', '0.000;gaps=0',
    '0;cov=3', '3', '3;cQv=20', '20', '20;del=0;ins=0;sub=0']

['unitig_5\t.\tregion\t2201\t2300\t0.00\t+\t.\tcov2=10.860',
    '1.217;gaps=0', '0;cov=8', '11', '13;cQv=20', '20', '20;del=0;ins=0;sub=0']

I need to pull lists and put them into a new file if cov2= (part of the first column above) is equal to some number greater than some specified integer (say 140), so then in that case the two lists above wouldn't be accepted.

How would I set it up to check which lists meet this qualification and put those lists to a new file?

2
  • 1
    Is cov2=... always at the end of the first string? Apr 10, 2015 at 15:27
  • Yes it is, the end of each first string
    – Russ
    Apr 13, 2015 at 2:43

4 Answers 4

0

You can use regex :

>>> l=['unitig_5\t.\tregion\t401\t500\t0.00\t+\t.\tcov2=3.000', '0.000;gaps=0',
...     '0;cov=3', '3', '3;cQv=20', '20', '20;del=0;ins=0;sub=0']

>>> import re
>>> float(re.search(r'cov2=([\d.]+)',l[0]).group(1))
3.0

The pattern r'cov2=([\d.]+)' will match and combination of digits (\d) and dot with length 1 or more. then you can convert the result to float and compare :

>>> var=float(re.search(r'cov2=([\d.]+)',l[0]).group(1))
>>> var>140
False

Also as its possible that your regex doesn't match the pattern you can use a try-except to handle the exception :

try :

    var=float(re.search(r'cov2=([\d.]+)',l[0]).group(1))
    print var>140

except AttributeError:
    #print 'the_error_message'
0

I would first split the first string by tabs "\t", which seems to separate the fields. Then, if cov2 is always the last fild, further parsing would be easy (cut of "cov2=", then convert the remainder to float and compare.

If not necessarily the last field, a simple search for the start should be sufficient. Of course, complexity could be increased indefinitively if error-checking or a more tolerant search is required.

0
lst = [ ['unitig_5\t.\tregion\t401\t500\t0.00\t+\t.\tcov2=3.000', '0.000;gaps=0',
    '0;cov=3', '3', '3;cQv=20', '20', '20;del=0;ins=0;sub=0'],
        ['unitig_5\t.\tregion\t2201\t2300\t0.00\t+\t.\tcov2=10.860',
    '1.217;gaps=0', '0;cov=8', '11', '13;cQv=20', '20', '20;del=0;ins=0;sub=0'], ]

filtered_list = [ l for l in lst if re.match('.*cov2=([\d.]+$'), l) ]
0

You could extract the float value using rsplit if all the first elements contain the substring:

for row in list_of_rows:
    if float(row[0].rsplit("=",1)[1]) > 140:
            # write rows

If you don't actually need every row you should do it when you first read the file writing as you go.

with open("input.csv") as f, open("output.csv", "w") as out:
    r = csv.reader(f)
    wr = csv.writer(out)
    for row in r:
        if float(row[0].rsplit("=", 1)[1]) > 140:
            wr.writerows(row)
4
  • When I try this I get a list index out of range error for the if statement
    – Russ
    Apr 13, 2015 at 2:40
  • print the row before the if and show me what it looks like Apr 13, 2015 at 9:58
  • It looks the same as what I put in the original post. It has the items in the list like this: ['unitig_19\t.\tregion\t13821\t13853\t0.00\t+\t.\tcov2=1.818', '0.386;gaps=0', '0;cov=1', '2', '2;cQv=20', '20', '20;del=0;ins=0;sub=0']
    – Russ
    Apr 13, 2015 at 13:34
  • If you print float(row[0].rsplit("=", 1)[1]) you will see it works on the list above so that cannot be what is happening Apr 13, 2015 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.