简体   繁体   English

NameError:名称“拆分”未定义

[英]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? 我正在运行python脚本,如下所示,第二种方式是用逗号(“,”)分隔的输入..am试图基于“,”分割这种方式,并继续将每个filter_log的输出附加到每个字符串上,以output.txt开头1.)这样做时是否出现以下错误? 2.)how to keep appending to output.txt for each search string passed in second argument? 2.)对于第二个参数传递的每个搜索字符串,如何保持附加到output.txt?

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])

You have a syntax error ( EDIT: Not a syntax error, but simply a mistake). 您有语法错误( 编辑:不是语法错误,而只是一个错误)。 You meant: 你的意思是:

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

  1. import does not work like C's include . import 不能像C的include 那样工作。 It creates a module object with the name of the module which you can use to reference attributes, ie use string.split(sys.argv[2], ',') and not split(sys.argv[2], ',') 它使用模块名称创建一个模块对象 ,可用于引用属性,即使用string.split(sys.argv[2], ',')而不是split(sys.argv[2], ',')
  2. Almost all the functions in the string module are deprecated. string模块中几乎所有功能均已弃用。 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? string = split(",",sys.argv[2])您是否意识到执行此分配后,您将不再能够引用string模块? Never use the name of built-in function/types/modules as names of variables! 切勿将内置函数/类型/模块的名称用作变量的名称!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM