user2639990
user2639990

Reputation:

NameError: name 'split' is not defined

I am running the python script as follows,the second arugment is a comma(",") seperated input..am trying to split this arugment based on "," and keep appending the output of each filter_log for each string to output.txt 1.)am getting the below error while doing so? 2.)how to keep appending to output.txt for each search string passed in second argument?

ERROR:-

string = split(",",sys.argv[2])
NameError: name 'split' is not defined

USAGE:-

python test.py input.log loc\modem,loc\data

CODE:-

import sys
import string

def filter_log(input_file, output_file, strs):
    with open(input_file, "r") as input, open(output_file, "w") as output:
        output.writelines(filter(lambda x: all([s in x for s in strs]), input.readlines()))

# here is just searched for "Warning", add other stuff
#filter_log("input.txt", "output.txt", ["Warning"])

print sys.argv[1]
print sys.argv[2]
for arg in sys.argv[2]:
    string = split(",",sys.argv[2])
    filter_log(sys.argv[1], "output.txt", ["Warning",string])

Upvotes: 0

Views: 34292

Answers (2)

Ofir Israel
Ofir Israel

Reputation: 3913

You have a syntax error (EDIT: Not a syntax error, but simply a mistake). You meant:

string = sys.argv[2].split(",")

Upvotes: 11

Bakuriu
Bakuriu

Reputation: 101959

  1. import does not work like C's include. It creates a module object with the name of the module which you can use to reference attributes, i.e. use string.split(sys.argv[2], ',') and not split(sys.argv[2], ',')
  2. Almost all the functions in the string module are deprecated. Strings have methods, use them! sys.argv[2].split(',')
  3. string = split(",",sys.argv[2]) do you realize that after this assignment is executed you will not be able to reference the string module anymore? Never use the name of built-in function/types/modules as names of variables!

Upvotes: 4

Related Questions