python - AttributeError:Python 代码中的 __getitem__
问题描述
我正在尝试打开一个名为filteredApps.txt 的文本文件,其中包含“app1.ear、app2.ear、app3.ear、app4.ear”作为新行,将其传递给一个列表,然后将其与另一个列表进行比较。然后最后在 main() 方法中调用部署函数,但我得到 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()
解决方案
继续评论:
过滤应用程序.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']
推荐阅读
- python - 使用 python、ssh 和 os.open 在远程目录上打开一个文件(命名管道)
- java - 您如何将输入限制为“是”“否”
- c# - BitArray 到 byte[] 错误转换
- java - 处理当前消息时优雅关闭 Spring 集成应用程序
- html - 如何使用 HTML 元素正确编写表单
- php - (WordPress/UltimateMember) 钩子函数在自定义插件/子主题中不起作用
- c# - Json 反序列化为具有派生泛型类列表的对象
- reactjs - D3 React - 在使用新位置绘制路径之前无法删除路径图
- c++ - EPOLL 弹跳导致使用 EPOLLPRI 按下按钮时多次点击,边缘设置为“both”
- java - 我尝试使用多态性,但我的代码不能像我预期的那样工作,有人可以帮我解决这个问题吗?