PDA

View Full Version : [SOLVED] Read output of file and search for a string, python



Yukawa
May 28th, 2012, 06:42 PM
Hey everyone,

I'm very new in Python. I have to read output of the file, search for the string and read the information in that line.

H 1 6563A 40.092 3.1917 +Col 4438A 37.004 0.0026

Line look like that:

The search string is
H 1 6563A the information I need is in bold.

Please help

MG&TL
May 28th, 2012, 06:55 PM
You should probably start a new thread...but anyway:


for line in open("file"):
if "H 1 6563A" in line:
split_string = line.split()
result_string = split_string[3] + " " + split_string[4]
print result_string


Correct me if I'm wrong.

nothingspecial
May 28th, 2012, 07:00 PM
You should probably start a new thread...

You are right.

But since Yukawa didn't, I have :)

Yukawa
May 28th, 2012, 07:41 PM
You should probably start a new thread...but anyway:


for line in open("file"):
if "H 1 6563A" in line:
split_string = line.split()
result_string = split_string[3] + " " + split_string[4]
print result_string


Correct me if I'm wrong.

Thank you for such a quick answer. This works jut fine when string is at the beginning of the line, but sometimes is in the middle? What to do in that case?

sanderj
May 28th, 2012, 07:56 PM
So, do you mean the two 'words' after "6563A" anywhere in the string. If so:


import string

line = "blabla blabla bla H 1 6563A 40.092 3.1917 +Col 4438A 37.004 0.0026"
searchstring = "6563A"

split_string = line[string.find(line,searchstring):].split()

result_string = split_string[1] + " " + split_string[2]
print result_string


with this result:


sander@R540:~$ python stringding.py
40.092 3.1917
sander@R540:~$

Yukawa
May 28th, 2012, 08:34 PM
So, do you mean the two 'words' after "6563A" anywhere in the string. If so:


import string

line = "blabla blabla bla H 1 6563A 40.092 3.1917 +Col 4438A 37.004 0.0026"
searchstring = "6563A"

split_string = line[string.find(line,searchstring):].split()

result_string = split_string[1] + " " + split_string[2]
print result_string


with this result:


sander@R540:~$ python stringding.py
40.092 3.1917
sander@R540:~$

Thanks mate it is working now

Yukawa
May 28th, 2012, 10:43 PM
OK another question. If I wonna perform that in multiple files, with same extension. How I can do that?

MG&TL
May 28th, 2012, 10:52 PM
Using glob.glob would be one solution. http://docs.python.org/library/glob.html#glob.glob



for files in glob.glob('/path/to/files/*.txt'):
for lines in open(files):
#Do stuff with lines here.


EDIT: although yeah, a quick google got that.

sanderj
May 28th, 2012, 10:52 PM
OK another question. If I wonna perform that in multiple files, with same extension. How I can do that?

... I would advice you to read something on Python. It now feels like we're doing your homework.

Yukawa
May 28th, 2012, 11:18 PM
So, do you mean the two 'words' after "6563A" anywhere in the string. If so:


import string

line = "blabla blabla bla H 1 6563A 40.092 3.1917 +Col 4438A 37.004 0.0026"
searchstring = "6563A"

split_string = line[string.find(line,searchstring):].split()

result_string = split_string[1] + " " + split_string[2]
print result_string


with this result:


sander@R540:~$ python stringding.py
40.092 3.1917
sander@R540:~$



Using glob.glob would be one solution. http://docs.python.org/library/glob.html#glob.glob



for files in glob.glob('/path/to/files/*.txt'):
for lines in open(files):
#Do stuff with lines here.


EDIT: although yeah, a quick google got that.
I was doing that, found it on the google, but I was having some other mistake in the code, so when i tried this in the first place it didn't work. Don't worry this is not homework, no marks no grades.

Thanks once more.