V Shriram
V Shriram

Reputation: 73

python calling arguments of function into another script

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.

Upvotes: 0

Views: 122

Answers (2)

A. Smoliak
A. Smoliak

Reputation: 448

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

Upvotes: 0

A.Queue
A.Queue

Reputation: 1572

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(...).

Upvotes: 1

Related Questions