Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9048397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:08:49+00:00 2026-06-16T12:08:49+00:00

I am using Python to generate some XML files which are used by another

  • 0

I am using Python to generate some XML files which are used by another device. I have a solution which works but I am trying to improve it. One of the things which the XML must contain is a schedule. Here is what the entry for Monday should look like in XML.

<Monday open="08:00" close="17:00" />

I have no problems generating this with ElementTree in Python but right now I am stuck having a line of code for each day. So a snippet of my Python code looks like this:

SubElement(schedule, 'Monday', open='08:00', close='17:00')
SubElement(schedule, 'Tuesday', open='08:00', close='17:00')
.
.
.
SubElement(schedule, 'Sunday', open='08:00', close='17:00')

The XML file will wind up having multiple schedules in it and typing all of that out seems needlessly repetitive. Instead of this I wanted to create a function which iterates over a list to build up the parameters to pass to SubElement. As part of that function I created this string to replace the times in my SubElement call.

"open=" + "\'08:00\'" + ", " + "close=" + "\'17:00\'"

If I print this out it looks identical to what I pass to SubElement when I type out the code the long way. However it clearly isn’t the same as I get the following error when I try to run the script.

File "C:\Python27\lib\xml\etree\ElementTree.py", line 528, in SubElement
   attrib = attrib.copy()
AttributeError: 'str' object has no attribute 'copy'

I have looked at the SubElement code and it seems to be expecting a dictionary. However, when I type things out the long way I don’t appear to be supplying a dictionary as input then either so I can’t see why the function should accept that input but not the string which appears, to me, to be equivalent. Is there any way to do what I am trying to do or am I stuck with a lot of repetitive lines of code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:08:50+00:00Added an answer on June 16, 2026 at 12:08 pm

    You should pass a dictionary of attributes instead of “string of attributes” to SubElement, something like:

    SubElement(schedule, 'Sunday', {'open': '08:00', 'close': '17:00'})
    

    or

    times = {'open': '08:00', 'close': '17:00'}
    SubElement(schedule, 'Sunday', times)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Python to generate some data and have some code like this
In Python, what is the best way to generate some random number using a
I'm using ElementTree to generate some HTML, but I've run into the problem that
I'm trying to generate some LaTeX code which from thereon should generate PDF documents.
I'm trying to have a script to generate some makefiles for me. I want
I'm using a small Python script to generate some binary data that will be
I'm using python 2.7, and trying to run some CPU heavy tasks in their
I am using sage maths (which uses Python(more or less)) to do some work
I have an HTML form and I am using Python to generate a log
I am currently writing a Python script and trying to dynamically generate some arguments.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.