简体   繁体   English

NameError: 名称 'rindex' 未定义

[英]NameError: name 'rindex' is not defined

I get this error when calling my python script from command line, passing in an input string (parameter 1) and a blank string (parameter 2) that will store my output from parsing this input string in my module从命令行调用我的 python 脚本时出现此错误,传入一个输入字符串(参数 1)和一个空白字符串(参数 2),它将存储我在模块中解析此输入字符串的输出

C:\Windows\system32>set project_name=

C:\Windows\system32>echo "%project_name%" " "

C:\Windows\system32>"C:\Python27\python" "C:\IR125422\GetProject1.py" "#p=/Product Delivery/Product Delivery.pj#s=Team P rojects/Team Projects.pj#s=Platform Pack/Platform Pack.pj#s=Runtime/Runtime.pj"   project_name

Traceback (most recent call last):   File "C:\IR125422\GetProject1.py", line 5, in <module>
    startpos = rindex("/",stext) NameError: name 'rindex' is not defined

My python program takes the string staring with "#p" and locates the last substring "/" in the string, then copies into the string project_name the remainder of this string from the last "/" to the end of the string.我的python程序采用以“#p”开头的字符串并定位字符串中的最后一个子字符串“/”,然后将该字符串的其余部分从最后一个“/”复制到字符串的末尾。 So after the program is run project_mane should contain "runtime.pj"所以程序运行后,project_mane 应该包含“runtime.pj”

Here is the program这是程序

import os, sys
stext = sys.argv[1]
rtext =  sys.argv[2]
startpos = rindex("/",stext)
rtext = stext(startpos+1,len(rtext))
print "Extracting project"
print rtext;

However it seems that the string method rindex is not recognised.但是,似乎无法识别字符串方法 rindex。 Do I need to add a module to my "Import" section?我需要在“导入”部分添加模块吗? I thought that this was not required for string handling in python as it is intrinsic to python我认为这不是 python 中的字符串处理所必需的,因为它是 python 固有的

rindex is not available as a global function, but rather as a method of the str class. rindex不能用作全局函数,而是用作str类的方法。 The usage is str.index(self, sub[, start[, end]]) .用法是str.index(self, sub[, start[, end]]) So if you replace the line:因此,如果您替换该行:

startpos = rindex("/",stext)

with either of the following two:具有以下两个之一:

startpos = str.rindex(stext, "/")
startpos = stext.rindex("/")

the line in question should work.有问题的线路应该工作。

Note however that the following line also contains an error:但是请注意,以下行也包含错误:

rtext = stext(startpos+1,len(rtext))

As it is currently, you would be calling stext as if it were a function, rather than indexing it as an iterable.就目前而言,您会像调用函数一样调用 stext,而不是将其索引为可迭代对象。 Try the following instead:请尝试以下操作:

rtext = stext[startpos+1:]

This will result in a " slice " of the original string, starting at startpos+1 and ending at the end of the string.这将导致原始字符串的“ 切片”,从 startpos+1 开始并在字符串的末尾结束。 So your complete code, without further modification would be:因此,无需进一步修改,您的完整代码将是:

import os, sys
stext = sys.argv[1]
rtext =  sys.argv[2]
startpos = stext.rindex("/") # This line changed
rtext = stext[startpos+1:]   # This line changed
print "Extracting project"
print rtext;

Giving the output:给出输出:

Extracting project
Runtime.pj

Edited to add:编辑添加:

As suggested by Two-Bit Alchemist, your code can also be cleaned up a bit.正如两位炼金术士所建议的那样,您的代码也可以稍微清理一下。 The initial assignment to rtext does nothing and can be omitted.rtext的初始赋值什么也不做,可以省略。 The print statements can be combined by adding a newline inside the string and using format , or if you want to somehow capture and use the output in DOS or PowerShell you might even want to omit the first print statement altogether.可以通过在字符串中添加换行符并使用format来组合打印语句,或者如果您想以某种方式捕获和使用 DOS 或 PowerShell 中的输出,您甚至可能想要完全省略第一个打印语句。 This may also be a good moment to make the print statements Python3-friendly by adding parentheses.这也可能是通过添加括号使print语句对 Python3 友好的好时机。 Additionally, Two-Bit Alchemist suggested to calculate rtext more directly:此外,两位炼金术士建议更直接地计算 rtext:

rtext = stext.split('/')[-1]

Which splits the string into a list at each '/' and then takes the last element, corresponding to the string portion after the final '/' just like the current solution.它将字符串拆分为每个 '/' 处的列表,然后取最后一个元素,对应于最终 '/' 之后的字符串部分,就像当前的解决方案一样。 Finally, the os import is unused and the semicolon after the second print statement is superfluous, therefore both can be omitted.最后, os导入没有使用,第二个打印语句后面的分号是多余的,因此两者都可以省略。 The cleaned up code would be:清理后的代码将是:

import sys
stext = sys.argv[1]
rtext = stext.split('/')[-1]
print(rtext)

Or even more compact:或者更紧凑:

import sys
print(sys.argv[1].split('/')[-1])

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

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