我正在尝试使用Python 2.7编写一个应用程序,它允许用户打开一个对话框,选择一个文件,并将它读入某种结构(ArrayList,List等)。 ..)开放给这里的建议,然后从数据中找到基本的统计测量(比如平均值,标准偏差等),并输出原始文件以XML格式的统计测量摘要。只是想知道如何完成这个最好的方法。我有打开窗口的代码,允许用户从另一个网站选择一个文件,但不知道如何使用它来将所选文件传递到将读取所选XML文件的函数中。与Java和Python有关的XML问题
下面是窗口中的代码:
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()
我想有更多的通功能。
我有一个版本在Java中的XMLReader(即在BlueJ的运行,因此不知道是否会耗尽它侧)代码:
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);
}
}
有一件事我不喜欢关于Java代码是我必须包含文件的路径。我想允许用户选择Java版本中的文件。我从java开始的原因是我有更多的经验(但不多)。要创建应用程序,最好的方法是什么,Python或Java,并根据哪一个帮助开始排序的问题将不胜感激。
我更喜欢从我认为它看起来比Java更容易的Python,因为您不必担心声明变量类型,语法更容易遵循。但是像大多数程序一样,这并不完全是为了我自己的用途(我会指出,我没有为它付钱,它的朋友想把它添加到他正在为俱乐部运行的网站上)。 至于输出文件,如果我能以某种方式将统计信息附加到原始xml文件中,那将是理想的解决方案。因为这部分的输出是成为他正在研究的内容的输入。 – Rebel 2010-07-23 09:38:13
您确定要将统计数据添加到旧文件中吗?我认为制作一个新文件(可能是XML格式)会更好,并让你的朋友进行分析。 此外,你确定你想使用XML与你的朋友沟通?对于这个目的来说,这有点笨重,特别是如果你没有那么多的数据要发送。你可以写一个纯文本文件,这可能更容易处理。 如果您确定要使用XML,请查看http://www.postneo.com/projects/pyxml/中关于如何使用'xml.dom.minidom'的示例。 – katrielalex 2010-07-23 09:46:43