2013-07-11 121 views
0

作爲名稱狀態我想創建一個XML表使用Python來存儲我正在使用的代碼的數據。當我嘗試將我的數字作爲字符串數據擠入XML時,我的問題就出現了。以下是我有:Python數字字符串在XML創建

print "pyClicker2.0 - Manual Input Mode\nPlease set two coordinate pairs..." 
x1 = raw_input("What is x1: ") 
y1 = raw_input("What is y1: ") 
x2 = raw_input("what is x2: ") 
y2 = raw_input("what is y2: ") 

#pyClickerXML - XML Out 
root = Element("coordinates") 
tree = ElementTree(root) 
x1Elm = Element("x1") 
y1Elm = Element("y1") 
x2Elm = Element("x2") 
y2Elm = Element("y2") 

x1Elm.text = x1.tostring() 
y1Elm.text = y1.tostring() 
x2Elm.text = x2.tostring() 
y2Elm.text = y2.tostring() 

tree.write(open("c:\\users\namehere\Desktop\coord_man.xml", "wt")) 

這是我的錯誤:

Traceback (most recent call last): 
File "<pyshell#4>", line 1, in <module> 
man() 
File "C:/Users/Zilvarael/Desktop/Folder of CodeMonkey/piSrc/pyclickerXML.py", line 36, in man 
x1Elm.text = x1.tostring() 
AttributeError: 'str' object has no attribute 'tostring' 

回答

1

你這樣做:

x1 = raw_input("What is x1: ") 
... 
x1Elm.text = x1.tostring() 

raw_input功能已經返回一個字符串,所以你不應該」 t需要轉換它。

+0

生成工作表,但沒有任何座標數據僅存在於根元素中。 – CodeMonkeyAlx

+1

對於所有的疑惑,我錯過了每個xy值的root.append(varName)屬性! - 謝謝比爾! – CodeMonkeyAlx