[Tutor] Basic ElementTree - another question

Don Jennings dfjennings at gmail.com
Tue Feb 25 06:34:01 EST 2020


> On Feb 25, 2020, at 12:45 AM, Phil <phillor9 at gmail.com> wrote:
> 
> On 25/2/20 1:00 pm, Don Jennings wrote:
>>> On Feb 24, 2020, at 7:54 PM, Phil <phillor9 at gmail.com> wrote:
>>> 
>>> <cut>
> 
>>> This is what I want to achieve:
>>> 
>>>   <wpt lat="-32.506533000" lon="137.740017000">
>>>     <name>1</name>
>>>     <cmt>1</cmt>
>>>     <desc>1</desc>
>>>   </wpt>
>>> 
>>> Hi, Phil. When you add keyword arguments (e.g. lat and lon), those become attributes in the resulting start-tag(s). Instead, assign the value to the text attribute of the element:
>>> 
>>>>> import xml.etree.ElementTree as ET
>>>>> root = ET.Element("root")
>>>>> child = ET.SubElement(root, "child", attr="some attribute")
>>>>> child.text = “content"
>>>>> ET.tostring(root)
>> b'<root><child attr="some attribute">content</child></root>’
> 
> Thanks Don, I appreciate your reply but after experimenting with what you have provided I still cannot get the result that I need. Would it be possible for you to provide code for the two lines that start with "<wpt lat=" and the next line "<name>1</name>"? Hopefully that will enable me to proceed with this section of the project.

Please paste the code which you’ve tried, so we can see what’s happening, thanks.

Best,
Don



More information about the Tutor mailing list