![](/img/trans.png)
[英]Python - ConfigParser - AttributeError: ConfigParser instance has no attribute '__getitem__'
[英]AttributeError: __getitem__ in Python code
我正在嘗試打開一個名為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()
從評論繼續:
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']
在遍歷數據之前嘗試讀取文件
appToDeploy = open("filteredApps.txt","r")
contents = appToDeploy.readlines()
appToDeploy.close()
for deploy in contents:
filteredAppsList.append(deploy)
嘗試按以下方式使用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(' ')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.