我正在嘗試使用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