我試圖使用ElementTree從.config文件中獲取數據。這個文件的結構是這樣的,例如:使用ElementTree解析.config文件中的數據時出錯
<userSettings>
<AutotaskUpdateTicketEstimatedHours.My.MySettings>
<setting name="Username" serializeAs="String">
<value>AAA</value>
</setting>
我的代碼是這樣的:
import os, sys
import xml.etree.ElementTree as ET
class Init():
script_dir = os.path.dirname(__file__)
rel_path = "app.config"
abs_file_path = os.path.join(script_dir, rel_path)
tree = ET.parse(abs_file_path)
root = tree.getroot()
sites = root.iter('userSettings')
for site in sites:
apps = site.findall('AutotaskUpdateTicketEstimatedHours.My.MySettings')
for app in apps:
print(''.join([site.get('Username'), app.get('value')]))
if __name__ == '__main__':
handler = Init()
然而,當我運行此代碼,我得到:
Traceback (most recent call last):
File "/Users/AAAA/Documents/Aptana/AutotaskUpdateTicketEstimatedHours/Main.py", line 5, in <module>
class Init():
File "/Users/AAA/Documents/Aptana/AutotaskUpdateTicketEstimatedHours/Main.py", line 16, in Init
print(''.join([site.get('Username'), app.get('value')]))
TypeError: sequence item 0: expected string, NoneType found
我」我做錯了原因這個錯誤?
(我的問題似乎訪問我config.file的樹結構正確)
@perror我真的不明白你爲什麼會認爲這是重複的.. – feners
我同意,我誤解了你的問題。看起來'用戶名'可能是'NoneType',那就是問題所在。抱歉。 – perror
@perror是啊,但我相信它是因爲我沒有正確訪問樹結構,有任何幫助嗎? – feners