Python遍历XML属性写入多文件及属性赋值类型错误解决
Hey there! Let's tackle your two XML processing tasks with Python and fix that frustrating error you ran into.
For this task, Python's built-in xml.etree.ElementTree module is perfect—it’s lightweight and gets the job done without extra dependencies. Here’s a step-by-step example:
Example Workflow:
- Parse your source XML file
- Locate the target
<Traces>element (adjust the path if it’s nested deeper) - Iterate over its attributes, then write each attribute’s data to a separate file (or structure output files however you need)
import xml.etree.ElementTree as ET # Parse your input XML tree = ET.parse("your_source.xml") root = tree.getroot() # Find the <Traces> element traces_element = root.find("Traces") # Iterate over all attributes and write to individual files for attr_key, attr_value in traces_element.attrib.items(): with open(f"trace_attr_{attr_key}.txt", "w") as output_file: output_file.write(f"Attribute: {attr_key}\nValue: {attr_value}")
If your goal is to generate multiple modified XML files (rather than just attribute value text files), you can combine this with the logic from your second task—modify attributes in loops and save each variant as a new XML file.
That error is telling you exactly what’s wrong: XML element attributes only accept string values, but you’re passing an integer (i). The fix is super simple—just convert your integer variables to strings using str() before assigning them with set().
Here’s a complete example for iterating over ranges of TS, TW, and D values and saving modified XML files:
import xml.etree.ElementTree as ET # Parse the original XML (we'll reuse this base for each variant) base_tree = ET.parse("your_source.xml") base_root = base_tree.getroot() base_traces = base_root.find("Traces") # Define your value ranges (adjust these to your needs) ts_values = range(5, 12) # TS from 5 to 11 inclusive tw_values = range(3, 8) # TW from 3 to 7 inclusive d_values = range(18, 23) # D from 18 to 22 inclusive # Iterate over all combinations of values for ts in ts_values: for tw in tw_values: for d in d_values: # Create a copy of the original <Traces> element to modify modified_traces = base_traces.copy() # Convert integers to strings BEFORE setting the attributes modified_traces.set("TS", str(ts)) modified_traces.set("TW", str(tw)) modified_traces.set("D", str(d)) # Update the tree with the modified element base_root.remove(base_traces) base_root.append(modified_traces) # Save the modified XML to a unique file output_filename = f"traces_ts{ts}_tw{tw}_d{d}.xml" base_tree.write(output_filename, encoding="utf-8", xml_declaration=True) # Restore the original element for the next iteration base_root.remove(modified_traces) base_root.append(base_traces)
Key Notes:
- Always convert numeric values to strings when setting XML attributes—this is the core fix for your error
- We reuse the base XML tree and restore the original
<Traces>element after each iteration to avoid messing up subsequent loops - If you don’t need all combinations (e.g., only iterate
TSwhile keepingTWandDfixed), just simplify the loops accordingly
内容的提问来源于stack exchange,提问作者Potato

