0

I have a txt file, from which I need to search a specific line, which is working, but in that line I need to strip the first 14 characters, and the part of the list element I am interested is dynamically generated during run time. So, scenario is I ran a script and the output is saved in output.txt, now I am parsing it, here is what I have tried

load_profile = open('output.txt', "r"
read_it = load_profile.read()
myLines = [ ]
for line in read_it.splitlines():
  if line.find("./testSuites/") > -1
   myLines.append(line)
print myLines

which gives output: ['*** Passed :) at ./testSuites/TS1/2013/06/17/15.58.12.744_14'] I need to parse ./testSuites/TS1/2013/06/17/15.58.12.744_14' part only and 2013 and est of the string is dynamically generated.

Could you please guide me what would be best way to achieve it?

Thanks in advance Urmi

Gabe
  • 84,912
  • 12
  • 139
  • 238
Urmi
  • 33
  • 4
  • `myLines.append(line[13:])`? Maybe you could also use the return of line.find instead of 13. – Maxime Chéramy Jun 18 '13 at 16:56
  • Thanks for the reply. I tried it now it gives result [' at ./testSuites/TS1/2013/06/17/15.58.12.744_14'], which is definitely 1 step closer, how can I save it in a variable as res1 = './testSuites/TS1/2013/06/17/15.58.12.744_14', as I need to pass this to another script? – Urmi Jun 18 '13 at 17:04

2 Answers2

6

Use slicing:

>>> strs = 'Passed :) at ./testSuites/TS1/2013/06/17/15.58.12.744_14'
>>> strs[13:]
'./testSuites/TS1/2013/06/17/15.58.12.744_14'

Update : use lis[0] to access the string inside that list.

>>> lis = ['*** Passed :) at ./testSuites/TS1/2013/06/17/15.58.12.744_14']
>>> strs = lis[0]
>>> strs[17:]       # I think you need 17 here
'./testSuites/TS1/2013/06/17/15.58.12.744_14'
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • Ashwini thanks for your reply. here my output is list element ['*** Passed :) at ./testSuites/TS1/2013/06/17/15.58.12.744_14'], of list len = 1, so, I how can I use string here? – Urmi Jun 18 '13 at 17:08
1

You are asking how to strip the first 14 characters, but what if your strings don't always have that format in the future? Try splitting the string into substrings (removing whitespace) and then just get the substring with "./testSuites/" in it.

load_profile = open('output.txt', "r")
read_it = load_profile.read()
myLines = [ ]
for line in read_it.splitlines():
    for splt in line.split():
        if "./testSuites/" in splt:
            myLines.append(splt)
print myLines

Here's how it works:

>>> pg = "Hello world, how you doing?\nFoo bar!"
>>> print pg
Hello world, how you doing?
Foo bar!
>>> lines = pg.splitlines()
>>> lines
["Hello world, how you doing?", 'Foo bar!']
>>> for line in lines:
...     for splt in line.split():
...             if "Foo" in splt:
...                     print splt
... 
Foo
>>> 

Of course, if you do in fact have strict requirements on the formats of these lines, you could just use string slicing (strs[13:] as Ashwini says) or you could split the line and do splt[-1] (which means get the last element of the split line list).

2rs2ts
  • 10,662
  • 10
  • 51
  • 95
  • Thank you guys:) both of your solution worked, so here what I have tried load_profile = open('output.txt', "r") read_it = load_profile.read() myLines = [ ] for line in read_it.splitlines(): for splt in line.split(): if "./testSuites/" in splt: myLines.append(splt) print myLines strs = myLines[0] print strs[12:] and got output which I needed /TS1/2013/06/17/15.58.12.744_14 THANK YOU:) – Urmi Jun 18 '13 at 17:22