Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AttributeError:\uuuu getitem\uuuu在Python代码中_Python_Python 2.7_Attributeerror - Fatal编程技术网

AttributeError:\uuuu getitem\uuuu在Python代码中

AttributeError:\uuuu getitem\uuuu在Python代码中,python,python-2.7,attributeerror,Python,Python 2.7,Attributeerror,我试图打开一个名为filteredApps.txt的文本文件,其中包含“app1.ear、app2.ear、app3.ear、app4.ear”作为新行,将其传递给一个列表,然后将其与另一个列表进行比较。然后最后在main()方法中调用deploy函数,但我在代码下面高亮显示的行中得到AttributeError:getitem: appNames = ['/opt/app1.ear', '/opt/app2.ear', '/opt/app3.ear', '/opt/app4.ear'] de

我试图打开一个名为filteredApps.txt的文本文件,其中包含“app1.ear、app2.ear、app3.ear、app4.ear”作为新行,将其传递给一个列表,然后将其与另一个列表进行比较。然后最后在main()方法中调用deploy函数,但我在代码下面高亮显示的行中得到AttributeError:getitem

appNames = ['/opt/app1.ear', '/opt/app2.ear', '/opt/app3.ear', '/opt/app4.ear']

def filteredApps():
    filteredAppsList = []
    appToDeploy = open("filteredApps.txt","r")
    for deploy in appToDeploy:   #Code breaks here
        filteredAppsList.append(deploy)
    return map(str.strip, filteredAppsList)

def main():
    finalListToDeploy = []
    listToDeploy = filteredApps() #Code breaks here as well

    for paths in appNames:
        for apps in listToDeploy:
            if apps in paths:
                finalListToDeploy.append(apps)
    deployApplication(finalListToDeploy)

if __name__ == "__main__":
    main()

在循环数据之前尝试读取文件

 appToDeploy = open("filteredApps.txt","r") 
 contents = appToDeploy.readlines()
 appToDeploy.close()

 for deploy in contents:     
     filteredAppsList.append(deploy)

继续评论:

filteredApps.txt:

app1
app2
app3
app4
因此

appNames = ['/opt/app1.ear', '/opt/app2.ear', '/opt/app3.ear', '/opt/app4.ear']

def filteredApps():
    filteredAppsList = []
    with open("filteredApps.txt","r") as appToDeploy:
      for apptodeploy in appToDeploy:
          # print(apptodeploy)
          filteredAppsList.append(apptodeploy)
    return map(str.strip, filteredAppsList)

def main():
    finalListToDeploy = []
    listToDeploy = list(filteredApps())
    for paths in appNames:
        for apps in listToDeploy:
            if apps in paths:
                # print(paths)
                finalListToDeploy.append(paths)
    return finalListToDeploy
    # deployApplication(finalListToDeploy)

if __name__ == "__main__":
    print(main())
['/opt/app1.ear', '/opt/app2.ear', '/opt/app3.ear', '/opt/app4.ear']
输出

appNames = ['/opt/app1.ear', '/opt/app2.ear', '/opt/app3.ear', '/opt/app4.ear']

def filteredApps():
    filteredAppsList = []
    with open("filteredApps.txt","r") as appToDeploy:
      for apptodeploy in appToDeploy:
          # print(apptodeploy)
          filteredAppsList.append(apptodeploy)
    return map(str.strip, filteredAppsList)

def main():
    finalListToDeploy = []
    listToDeploy = list(filteredApps())
    for paths in appNames:
        for apps in listToDeploy:
            if apps in paths:
                # print(paths)
                finalListToDeploy.append(paths)
    return finalListToDeploy
    # deployApplication(finalListToDeploy)

if __name__ == "__main__":
    print(main())
['/opt/app1.ear', '/opt/app2.ear', '/opt/app3.ear', '/opt/app4.ear']

请尝试使用open,如下所示:

import io
from io import open
with open('tfilteredApps.txt', 'r', encoding='utf-8') as file :    
    for deploy in file :
        filteredAppsList.append(deploy)
但是如果你把所有的应用程序名称都放在一行,那么pickle模块就是这样的:

import pickle
with open('tfilteredApps.txt', 'r', encoding='utf-8') as file :
    word = pickle.load(file)
filteredAppsList = word.split(' ')

这里的代码中断是什么意思?您会遇到什么错误?请发布完整的跟踪。哪里定义了
deployApplication
appToDeploy=open(“filteredaps.txt”,“r”)
不是正确的方法。我只是强调了jenkins说错误来自哪一行。代码在调用deployApplication()之前中断,因此与此问题无关。我现在正忙着测试它。给我一个sec@Sizwe怎么样?有进展吗?09:53:05回溯(最里面的最后一个):09:53:05文件“”,第129行,在?09:53:05文件“/home/jenkins/workspace/sizwe_deploy/bin/。/deploy/deployApplication.py”,第350行,在09:53:05主文件“/home/jenkins/workspace/sizwe_deploy/bin/。/deploy/deployApplication.py”第329行,在filteredApps 09:53:05中AttributeError:org.python.core.PyReflectedFunction的实例没有与地图有关的属性'strip'@Sizwe(str.strip,FilteredAppList)谢谢!我会尽快尝试并给出反馈