1
#!/usr/bin/python

import sys, re

fname = sys.argv[1]
barcodefname = sys.argv[2]

barcodefile = open(barcodefname, "r")
#list = open(barcodefname, "r").readlines(-1)

for barcode in barcodefile:
        barcode = barcode.strip()
        print "barcode: %s" % barcode
        outfname = "%s.%s" % (fname, barcode)
        outf = open(outfname, "w")
        handle = open(fname, "r")
        for line in handle:
                potential_barcode = line[:len(barcode)]
                if potential_barcode == barcode:
                        outseq = line[len(barcode):]
                        sys.stdout.write(outseq)
                        outf.write(outseq)
        handle.close()
        outf.close()
barcodefile.close()

The problem I have is that the second argument file looks something like this:

S1 djgbfgbf
S2 dkffbjfb
S3 lfjbvrid
....etc

I need to find a way to ignore the S1, S2, S3 at the beginning of each line and only match the following letters to the argument 1 file. On line 9 I was experimenting with creating a list somehow and reversing it but I'm not sure if that's the answer.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Justin P
  • 23
  • 3

2 Answers2

3

For a string like S1 djgbfgbf if you want to ignore the first part you can split the string and choose the last item,and also this string could be a line of file :

>>> s='S1 djgbfgbf'
>>> s.split()[-1]
'djgbfgbf'

For example if you have a file with the name in_file you can do the following list comprehension that its result is the last part of all your lines :

[line.split()[-1] for line in open('in_file')]

or you can loop over your file (is less efficient than list comprehension but could be more flexible):

for line in open('in_file'):
      last_part=line.split()[-1]
      #do stuff with last part
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • In order to apply this to each line of the barcodefile could I do something like bcfile = split(barcodefile)[-1] ? – Justin P Mar 23 '15 at 06:51
0

You can do as Kasra wrote. ALternatively if you are interested only in the last word(s), you can just split from the right on do only one split, rather than splitting to all words.

In [11]: a="some sentence last word"

In [12]: a.rsplit(maxsplit=1)
Out[12]: ['some sentence last', 'word']

In [13]: a.rsplit(maxsplit=1)[-1]
Out[13]: 'word'
Marcin
  • 215,873
  • 14
  • 235
  • 294