【发布时间】:2015-12-08 15:45:55
【问题描述】:
从命令行调用我的 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
我的 python 程序获取以“#p”开头的字符串,并在字符串中找到最后一个子字符串“/”,然后将这个字符串的剩余部分从最后一个“/”复制到字符串 project_name 到字符串的末尾.所以程序运行后project_mane应该包含“runtime.pj”
这是程序
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;
但是,似乎无法识别字符串方法 rindex。我需要在我的“导入”部分添加一个模块吗?我认为这不是 python 中的字符串处理所必需的,因为它是 python 所固有的
【问题讨论】:
-
"参数 2 将在我的模块中存储我解析此输入字符串的输出。"你到底要在那里传递什么,一个建议的内存位置?这既不是必需的,在 Python 的上下文中也没有任何意义。 (或者实际上是任何接受命令行参数的程序。)