I am trying to write an application using Python 2.7 that will allow the user to open a dialog box, pick a file, have it read into a structure of some sort (ArrayList, List etc...) open to suggestions here and then to find basic statistical measures from the data (things like mean, standard deviation etc...) and to output the original file with a summary of the statistical measurements in XML format. Just wondering how is the best way to accomplish this. I have code for opening a window to allow a user to pick a file (from another website) but not sure how to use it to pass the selected file into the function that will read the xml file selected.
Here is the code for the window:
from Tkinter import *
from tkMessageBox import *
from tkColorChooser import askcolor
from tkFileDialog import askopenfilename
def callback():
askopenfilename()
Button(text='Please Select File', command=callback).pack(fill=X)
mainloop()
pass
def quit(event):
if tkMessageBox.askokcancel('Quit','Do you really want to quit?'):
root.destroy()
I think there is more to the pass function.
I have a version of the XMLReader in Java (That is run in BlueJ so don't know if it will run out side of it) code:
import java.util.*;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLReader
{
public static void main(String argv[]) {
ArrayList XTimeStamp = new ArrayList();
ArrayList XY = new ArrayList();
ArrayList Price = new ArrayList();
try {
File file = new File("<PATH>\shares.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("shareprice");
System.out.println("Share Price");
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("timeStamp");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
String timeStamp = fstNm.item(0).getNodeValue();
XTimeStamp.add(timeStamp);
System.out.println("timeStamp : " + ((Node) fstNm.item(0)).getNodeValue());
NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("Price");
Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
NodeList lstNm = lstNmElmnt.getChildNodes();
String YValue = lstNm.item(0).getNodeValue();
Price.add(YValue);
System.out.println("Price : " + ((Node) lstNm.item(0)).getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(XTimeStamp);
System.out.println(Price);
XY.add (XTimeStamp);
XY.add (Price);
System.out.println(XY);
}
}
The one thing I don't like about the Java code is that I have to include the path of the file. I would like to allow the user to pick the file in the Java version as well. The reason I started with java is I have a little bit more experience (but not much). To create the application what is the best way forward, Python or Java and depending on which one any help on getting started sorting the issues would be greatly appreciated.