-2

i want my script to search in a specific file (text.txt) for "JOL:" ; "CRY:" ; "LAY:", if it doesn't find "JOL:" i want it to search for "CRY:" and if it doesn't find "CRY:" to look for "LAY:". The problem is that i don't know if it's possible to include an "Elif" with a "match" command, and if it's the case... How ?

import re, os

mykey = open("text.txt", "r")
take = open("take.txt", "w")
match = re.search ('"JOL":"(.+?).tr', text)
match2 = re.search('"CRY":"(.+?).tr', text)
match3 = re.search ('"LAY":"(.+?).tr', text)

for text in mykey:
    if match:
        print >> take, match.group(1)
    elif match:
        print >> take, match2.group(1)
    elif match:
        print >> take, match3.group(1)

Thanks

CarefullyAdvanced
  • 437
  • 1
  • 4
  • 15
  • why do you have `>>` in your `print` statement? – pad Nov 23 '14 at 16:49
  • 1
    You have 3 different regexp that go to 3 different `match` variables. Should you be testing something like _if I don't find a match with the first pattern, I try with **the other** match, and if I still don't find anything, I'd try with **the third** match?_ – Savir Nov 23 '14 at 16:50
  • 1
    @pad: That's how you print to a file in Python 2.x: http://stackoverflow.com/a/9316160/47453 – Bill Lynch Nov 23 '14 at 16:51
  • @BorrajaX Yes, i'm trying to do that. But how ? – CarefullyAdvanced Nov 23 '14 at 16:52

1 Answers1

1

It looks to me like the problem is that you're always testing if the text matches match:

So, let's simplify a bit. What happens with the following code?:

match = None
if match:
    # The interpreter will never get here because `match` is None, which
    # evaluates to False when in an `if`
    print >> take, match.group(1)
elif match:
    # This `elif` is still testing against `match`, which is still None,
    # therefore, evaluated to false. 
    print >> take, match2.group(1)
elif match:
    # Same drill here...
    print >> take, match3.group(1)

I'm guessing you want to do:

if match:
    print >> take, match.group(1)
elif match2:
    print >> take, match2.group(1)
elif match3:
    print >> take, match3.group(1)

EDIT:

Maybe the following code will help you understand what's happening. The code below transforms yours by doing two things:

  1. Read the input file line by line
  2. Match each line to each of the three expressions

With those changes, the code would look like:

import re, os

mykey = open("text.txt", "r")
take = open("take.txt", "w")

print "I have opened a file object to read stuff. That is: %s" % mykey
print "I have opened a file object to write stuff. That is: %s" % take

for text in mykey:
    print "I have read the line: %s" % text
    match = re.search('"JOL":"(.+?).tr', text)
    match2 = re.search('"CRY":"(.+?).tr', text)
    match3 = re.search('"LAY":"(.+?).tr', text)
    if match:
        print >> take, match.group(1)
    elif match2:
        print >> take, match2.group(1)
    elif match3:
        print >> take, match3.group(1)

If text.txt contains the following:

"JOL":"foo1".tr
"CRY":"bar1".tr
"LAY":"baz1".tr
"LAY":"baz2".tr
"CRY":"bar2".tr
"JOL":"foo2".tr

The contents found in take.txt after you run this script will be:

foo1"
bar1"
baz1"
baz2"
bar2"
foo2"

I have added some print statements that maybe will help you understand a bit what's going on. Check your terminal and see if that "extra debug" output helps you follow what's happening in the code.

You should also try to understand how file objects (input-output, in general) works in Python.

Savir
  • 17,568
  • 15
  • 82
  • 136
  • I've this error NameError : name "text" is not defined – CarefullyAdvanced Nov 23 '14 at 17:17
  • That's a different issue... You're using `text` in `match = re.search ('"JOL:"(.+?).tr', text)` before initializing the `text` variable anywhere before that line. Maybe it should be `mykey`? (dunno) Also, you're gonna have another problem in the `for text in take` loop. Your `take` variable is pointing to a file that is opened for write only (see the `open("take.txt", "w")` line). When you reach that line, you'll get an `IOError: File not open for reading` – Savir Nov 23 '14 at 17:24
  • I just copying the wrong code. The one i use now is in "my question" and i still get the same error. – CarefullyAdvanced Nov 23 '14 at 17:26
  • yeah, 'cuz you're doing the same thing **:-)** Your interpreter doesn't know what `text` is when it reaches `match = re.search ('"JOL:"(.+?).tr', text)` What is `text` supposed to be? The contents of `text.txt`? Because you don't have those anywhere in your code. The closest thing you have is `mykey`, but that's not the content of the `text.txt` file. Is the `text.txt` **file** object (see https://docs.python.org/2/library/stdtypes.html#bltin-file-objects ) – Savir Nov 23 '14 at 17:30
  • I didn't understand. How can i define "text" command then ? mykey is supposed to open and read "text.txt" so why it can't define the "content" of text.txt ? And above all, why does it work when there is only for text in mykey: if match: print >> take, match.group(1) without "elif" – CarefullyAdvanced Nov 23 '14 at 20:09
  • To read files, use `.read()`. The issue of why with `text` would work on some occasions is actually a good question, and I don't have an answer... – Savir Nov 23 '14 at 20:13
  • I'm totally lost. Can you please edit your first answer and put the right way to do it ? – CarefullyAdvanced Nov 23 '14 at 21:02
  • That's probably not what I should be doing (according to StackOverflow principles) but well... I hope the *EDIT* helps you understand what's going on – Savir Nov 23 '14 at 22:00
  • Glad I helped... Yeah, is important understanding that `open('whatever.txt')` doesn't give you the **contents** of the file, just a `file` object (which then needs to be read) **:-)** – Savir Nov 23 '14 at 22:22