我試圖向只接受XML的API發送請求。我使用elementtree.SimpleXMLWriter
來構建XML樹並將其存儲在一個StringIO對象中。這一切都很好,很棒。用於編碼URL的urllib.urlencode的替代
問題是我必須urlencode StringIO對象才能將其發送到API。但是,當我嘗試,我得到:
File "C:\Python27\lib\urllib.py", line 1279, in urlencode
if len(query) and not isinstance(query[0], tuple):
AttributeError: StringIO instance has no attribute '__len__'
顯然,這已爲an issue with Python討論。我只是想知道是否有任何其他內置函數用於urlencoding字符串,特別是不需要調用len()
以便我可以編碼此StringIO對象的函數。
謝謝!
PS:我打算使用StringIO以外的東西存儲XML對象,如果這是一個更簡單的解決方案。我只需要某種類型的「file」來存儲XML。