0

I am new to python and trying to call arguments of one function into another script. But I keep getting error. Below is the code: the input is a text file for the below function.

script1.py

def regex_parameters(value, arg):

    a = re.search(r'\d{1}-\d{1}', value)

    b = re.search(r'\d{1}-\d{1}', value)

    c = re.search(r'\d{1,4}( \w+){1,6},( \w+){1,3}\s\d{1,6}', value)

    d = re.search(r'\(?\b[2-9][0-9]{2}\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}\b', value)

    date = re.search(r'[A-z]{3,10}\s\d{1,2},\s\d{4}', value)

    return(value, arg)

script2.py

import script 1
from script1 import *

for i in arg:
    identity = regex_parameters(value, i)
    if value is not None:
        print(i, ":", value.group())
    else:
        clean = ""

i would like the output to be:

a = output of regex
b = output of regex

any help is much appreciated.

3
  • Please add the error output to your problem. Commented Feb 11, 2018 at 5:22
  • NameError: name 'arg' is not defined Commented Feb 11, 2018 at 5:38
  • i am sure i am doing a mistake in calling, but not sure - where the problem is! arg should take the values a,b,c,d and value is the search part of regex from text. thank you very much! Commented Feb 11, 2018 at 5:42

2 Answers 2

1

You didn't defined variable arg before the point when it is accessed in:

for i in arg: <---
    ...

Do something like this:

arg = [... , ... , ...]
for i in arg: <---
    ...

Another thing, value doesn't have a '.group()', because value is still a .

You assume value to be a Match Object, because that's what re.search() returns, but you have never did value = re.search(...).

Sign up to request clarification or add additional context in comments.

2 Comments

i am calling the variable text as: identity = regex_parameters(text, i) please note: text is a variable defined in script1.py the error i get is: NameError: name 'arg' is not defined
Edited my answer, please, put the entire error in your question. People really appreciate it in Stack Overflow.
0

If you're looking to parse command-line arguments you must import the sys package import sys in script2.py. Instead of arg at for i in arg: you must write for i in sys.argv: the result should be like this:

import script1
import sys
from script1 import *

for i in sys.argv:
    identity = regex_parameters(value, i)
    if value is not None:
        print(i, ":", value.group())
    else:
        clean = ""

Note that the first argument in sys.argv is going to be the filename, so if you want to avoid that you should splice the first argument like so: sys.argv[1:]

More on parsing Command-line arguments in Python

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.