Using the ElementTree module, write code to read in the “classes.csv” file and s
ID: 664922 • Letter: U
Question
Using the ElementTree module, write code to read in the “classes.csv” file and save the information in it as elements in your “classes.xml” file. This should produce this output:
New class added: I 201
New class added: I 210
New class added: L 311
New class added: M 550
New class added: A 200
New class added: A 325
And this file:
<?xml version="1.0" encoding="UTF-8"?>
<ClassList>
<Class>
<department abbr="I">INFO</department>
<title>
<number>201</number>
<name>Math Foundations of Informatics</name>
</title>
<credits>4</credits>
<days>MW</days>
<seats building="LH" room="102">90</seats>
</Class>
<Class>
<department abbr="I">INFO</department>
<title>
<number>210</number>
<name>Information Infrastructure I</name>
</title>
<credits>4</credits>
<days>MW</days>
<seats building="SB" room="015">75</seats>
</Class>
<Class>
<department abbr="L">BIOL</department>
<title>
<number>311</number>
<name>Genetics</name>
</title>
<credits>3</credits>
<days>TR</days>
<seats building="JH" room="A100">150</seats>
</Class>
<Class>
<department abbr="M">BIOL</department>
<title>
<number>550</number>
<name>Microbiology</name>
</title>
<credits>3</credits>
<days>MWF</days>
<seats building="JH" room="124">7</seats>
</Class>
<Class>
<department abbr="A">BUS</department>
<title>
<number>200</number>
<name>Foundations of Accounting</name>
</title>
<credits>3</credits>
<days>MW</days>
<seats building="HH" room="109">155</seats>
</Class>
<Class>
<department abbr="A">BUS</department>
<title>
<number>325</number>
<name>Cost Accounting</name>
</title>
<credits>3</credits>
<days>MW</days>
<seats building="HH" room="3030">40</seats>
</Class>
</ClassList>
Properly handle errors and validate any dangerous code in your file.
should be in python. link for classes.csv is here:
https://drive.google.com/file/d/0ByzqQ0F_j-zAanI5X2xFbGpUMVk/view?usp=sharing
Explanation / Answer
#Assignment 5, Group Work(1/2): New York Times RSS Feed #Brett Byron; Group 1 import urllib, xml.etree.ElementTree as ET #Main conn = urllib.urlopen("http://feeds.nytimes.com/nyt/rss/World") lines = conn.read() conn.close() root = ET.XML(lines) items = root.findall("channel/item") ###Part A ##for elem in items: ## print " " + elem.find("title").text + ": " + "( " + ## elem.find("{http://purl.org/dc/elements/1.1/}creator").text + ## " )" ###Part B ##namespaces = ['http://www.nytimes.com/namespaces/keywords/nyt_geo', 'http://www.nytimes.com/namespaces/keywords/nyt_org_all'] ##for elem in items: ## regions = ", ".join([item.text for item in elem.findall("category") if item.items()[0][1] in namespaces]) ## if "sinosphere" in elem.find("title").text.lower(): ## print " China: " + elem.find("title").text + ": " + ## elem.find("{http://purl.org/dc/elements/1.1/}creator").text + ## " )" ## else: ## print " " + regions + ": " + elem.find("title").text + ": " + ## elem.find("{http://purl.org/dc/elements/1.1/}creator").text + ## " )" ###Part C ##namespaces = ['http://www.nytimes.com/namespaces/keywords/nyt_geo', 'http://www.nytimes.com/namespaces/keywords/nyt_org_all'] ##for elem in items: ## regions = ", ".join([item.text for item in elem.findall("category") if item.items()[0][1] in namespaces]) ## if "sinosphere" in elem.find("title").text.lower(): ## print " China: " + elem.find("title").text + ": " + ## elem.find("{http://purl.org/dc/elements/1.1/}creator").text + ## " )" ## else: ## print " " + regions + ": " + elem.find("title").text + ": " + ## elem.find("{http://purl.org/dc/elements/1.1/}creator").text + ## " )" ## print "Link: " + elem.find("link").text ###Part D ##found = False ##keyword = raw_input("Please enter a category to search for: ") ##namespaces = ['http://www.nytimes.com/namespaces/keywords/nyt_geo', 'http://www.nytimes.com/namespaces/keywords/nyt_org_all'] ##for elem in items: ## for each in elem: ## if each.text != None: ## if keyword.lower() in each.text.lower(): ## regions = ", ".join([item.text for item in elem.findall("category") if item.items()[0][1] in namespaces]) ## if "sinosphere" in elem.find("title").text.lower(): ## print " China: " + elem.find("title").text + ": " + ## elem.find("{http://purl.org/dc/elements/1.1/}creator").text + ## " )" ## else: ## print " " + regions + ": " + elem.find("title").text + ": " + ## elem.find("{http://purl.org/dc/elements/1.1/}creator").text + ## " )" ## print "Link: " + elem.find("link").text ## found = True ## else: ## found = False ##if not found: ## print "No matches found." #Assignment 5, Group Work: Creating XML Files #Brett Byron; Group 1 import csv, os, xml.etree.ElementTree as ET #Add class to xml file def add_xml_class(dept, abbr, num, name, credit, days, seats, building, room): root = ET.parse(source="classes.xml") elements = root.getiterator() class_list = elements[0] new_class = ET.SubElement(class_list, "Class") department = ET.SubElement(new_class, "department") department.text = dept department.set("abbr", abbr) title = ET.SubElement(new_class, "title") class_num = ET.SubElement(title, "number") class_num.text = num class_name = ET.SubElement(title, "name") class_name.text = name class_cred = ET.SubElement(new_class, "credits") class_cred.text = credit class_days = ET.SubElement(new_class, "days") class_days.text = days class_seats = ET.SubElement(new_class, "class") class_seats.set("building", building) class_seats.set("room", room) root.write("classes.xml") print "New class added: " + abbr + " " + num classes = open(os.path.join(os.getcwd(), "classes.csv"), "r") read = csv.reader(classes) read.next() for row in read: abbr, number = row[0].split() department = row[1] name = row[2] credit = row[3] days = row[4] seats = row[-1] building, room = row[5].split() add_xml_class(department, abbr, number, name, credit, days, seats, building, room)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.