2013-03-14 52 views
0

期望的結果: 我試圖生成一個可縮放的矢量圖形,顯示我們的救護車的位置和一個顏色,指示救護車是否繁忙,停止服務,宿舍等。我們在路上的醫護人員可以將此視爲網頁,並將我們所有救護車的配置視爲地圖。他們一眼就能看到哪些救護車正忙着哪些是免費的。python和ElementTree編寫一行長輸出

我的方法: 我使用python 2.7與pyodbc和xml.etree。 我正在查詢我們的數據庫,以確定最近的救護車狀態,循環訪問陣列並生成數據的svg。

問題: 當輸出寫出到文本中,並使用記事本打開.svg文件時,我看到有一行很長的輸出。我想它是嵌套的輸出。如果你看看我的代碼,你會看到我只測試了兩輛救護車。但我的系統中有50輛救護車。我不能有一個長長的數千個字符。

問題: 我如何讓python/ElementTree在輸出中寫出換行符?如下圖所示

代碼:

import pyodbc 
from xml.etree import ElementTree as et 

cnxn = pyodbc.connect('DRIVER={SQL Server}; SERVER=my server; DATABASE=my database; UID=my user id; PWD=my password') 
cursor = cnxn.cursor() 
AmbulanceStatus = """ 
WITH Maxtimes AS 
(
SELECT 
    MAX(M_Manpower_PK) AS [FirstRecord] 
, PUN_UnitID 
FROM MManpower 
INNER JOIN PUnit ON M_kUnit = PUN_Unit_PK 
WHERE PUN_UnitID LIKE 'M__' 
OR PUN_UnitID LIKE 'FBA__' 
AND M_tTime > DATEADD(HOUR, -24, GETDATE()) 
GROUP BY PUN_UnitID 
) 
, 
MaxAttributes AS 
(
SELECT 
    M_Manpower_PK 
, M_tTime 
, M_Code 
, CASE 
    WHEN M_Code IN ('USTA', 'USER', 'USOL', 'USAR', 'USIZ', 'UF', 'USTR', 'USDP') THEN 'Working' 
    WHEN M_Code IN ('USAQ', 'USRQ') THEN 'Free' 
    WHEN M_Code IN ('USES', 'USSB', 'USAS', 'USRS', 'USLC') THEN 'Standby' 
    WHEN M_Code IN ('USOS') THEN 'OutofService' 
    ELSE 'NA' 
    END AS [Disposition] 
FROM MManpower 
INNER JOIN PUnit ON M_kUnit = PUN_Unit_PK 
WHERE PUN_UnitID LIKE 'M__' 
OR PUN_UnitID LIKE 'FBA__' 
AND M_tTime > DATEADD(HOUR, -24, GETDATE()) 
) 

SELECT 
    MaxTimes.firstRecord AS [RecordNumber] 
, MaxTimes.PUN_UnitID AS [Ambulance] 
, CONVERT(VARCHAR(20), MaxAttributes.M_tTime, 108) AS [FinalTimeStamp] 
, MaxAttributes.M_Code AS [UnitStatus] 
, MaxAttributes.Disposition AS [Disposition] 
FROM Maxtimes 
LEFT OUTER JOIN MaxAttributes ON Maxtimes.FirstRecord=MaxAttributes.M_Manpower_PK 
""" 

cursor.execute(AmbulanceStatus) 

Ambulances = cursor.fetchall() 

cursor.close() 

for ambulance in Ambulances: 
    print ambulance 

doc = et.Element('svg', width='792', height='612', version='1.1', xmlns='http://www.w3.org/2000/svg') 
#=============================================================================== 
# I have no idea how to implement the following 15 lines of code. I downloaded them from the internet. I have googled this question and someone believes the following lines of code can help write out carriage returns. 
#=============================================================================== 
# def indent(elem, level=0): 
# i = "\n" + level * " " 
# if len(elem): 
#  if not elem.text or not elem.text.strip(): 
#   elem.text = i + " " 
#  if not elem.tail or not elem.tail.strip(): 
#   elem.tail = i 
#  for elem in elem: 
#   indent(elem, level + 1) 
#  if not elem.tail or not elem.tail.strip(): 
#   elem.tail = i 
# else: 
#  if level and (not elem.tail or not elem.tail.strip()): 
#   elem.tail = i 
#=============================================================================== 
j = 0 
#=============================================================================== 
# medic 19 
#=============================================================================== 
while j < len(Ambulances): 
    if Ambulances[j][1] == 'M19' and Ambulances[j][4] == 'Free': 
     fillColor = 'aliceblue' 
     fillText = 'black' 
     et.SubElement(doc, 'circle', cx='520', cy='85', r='10', stroke='black', fill=fillColor) 
     text = et.Element('text', x='520', y='85', fill=fillText, style='font-family:Sans;font-size:12px;text-anchor:middle;dominant-baseline:top') 
     text.text = Ambulances[j][1] 
     doc.append(text) 

    elif Ambulances[j][1] == 'M19' and Ambulances[j][4] == 'Working': 
     fillColor = 'red' 
     fillText = 'white' 
     et.SubElement(doc, 'circle', cx='520', cy='85', r='10', fill=fillColor) 
     text = et.Element('text', x='520', y='85', fill=fillText, style='font-family:Sans;font-size:12px;text-anchor:middle;dominant-baseline:top') 
     text.text = Ambulances[j][1] 
     doc.append(text) 

    elif Ambulances[j][1] == 'M19' and Ambulances[j][4] == 'Standby': 
     fillColor = 'green' 
     fillText = 'white' 
     et.SubElement(doc, 'circle', cx='520', cy='85', r='10', fill=fillColor) 
     text = et.Element('text', x='520', y='85', fill=fillText, style='font-family:Sans;font-size:12px;text-anchor:middle;dominant-baseline:top') 
     text.text = Ambulances[j][1] 
     doc.append(text) 

    elif Ambulances[j][1] == 'M19' and Ambulances[j][4] == 'OutofService': 
     fillColor = 'black' 
     fillText = 'white' 
     et.SubElement(doc, 'circle', cx='520', cy='85', r='10', fill=fillColor) 
     text = et.Element('text', x='520', y='85', fill=fillText, style='font-family:Sans;font-size:12px;text-anchor:middle;dominant-baseline:top') 
     text.text = Ambulances[j][1] 
     doc.append(text) 

    elif Ambulances[j][1] == 'M19' and Ambulances[j][4] == 'NA': 
     fillColor = 'pink' 
     fillText = 'blue' 
     et.SubElement(doc, 'circle', cx='520', cy='85', r='10', fill=fillColor) 
     text = et.Element('text', x='520', y='85', fill=fillText, style='font-family:Sans;font-size:12px;text-anchor:middle;dominant-baseline:top') 
     text.text = Ambulances[j][1] 
     doc.append(text) 
#=============================================================================== 
# medic 16 
#=============================================================================== 
    elif Ambulances[j][1] == 'M16' and Ambulances[j][4] == 'Free': 
     fillColor = 'aliceblue' 
     fillText = 'black' 
     et.SubElement(doc, 'circle', cx='28.80024', cy='45.8372', r='10', stroke='black', fill=fillColor) 
     text = et.Element('text', x='28.80024', y='45.8372', fill=fillText, style='font-family:Sans;font-size:12px;text-anchor:middle;dominant-baseline:top') 
     text.text = Ambulances[j][1] 
     doc.append(text) 

    elif Ambulances[j][1] == 'M16' and Ambulances[j][4] == 'Working': 
     fillColor = 'red' 
     fillText = 'blue' 
     et.SubElement(doc, 'circle', cx='28.80024', cy='45.8372', r='10', fill=fillColor) 
     text = et.Element('text', x='28.80024', y='45.8372', fill=fillText, style='font-family:Sans;font-size:12px;text-anchor:middle;dominant-baseline:top') 
     text.text = Ambulances[j][1] 
     doc.append(text) 

    elif Ambulances[j][1] == 'M16' and Ambulances[j][4] == 'Standby': 
     fillColor = 'green' 
     fillText = 'white' 
     et.SubElement(doc, 'circle', cx='28.80024', cy='45.8372', r='10', fill=fillColor) 
     text = et.Element('text', x='28.80024', y='45.8372', fill=fillText, style='font-family:Sans;font-size:12px;text-anchor:middle;dominant-baseline:top') 
     text.text = Ambulances[j][1] 
     doc.append(text) 

    elif Ambulances[j][1] == 'M16' and Ambulances[j][4] == 'OutofService': 
     fillColor = 'black' 
     fillText = 'white' 
     et.SubElement(doc, 'circle', cx='28.80024', cy='45.8372', r='10', fill=fillColor) 
     text = et.Element('text', x='28.80024', y='45.8372', fill=fillText, style='font-family:Sans;font-size:12px;text-anchor:middle;dominant-baseline:top') 
     text.text = Ambulances[j][1] 
     doc.append(text) 

    elif Ambulances[j][1] == 'M16' and Ambulances[j][4] == 'NA': 
     fillColor = 'pink' 
     fillText = 'blue' 
     et.SubElement(doc, 'circle', cx='28.80024', cy='45.8372', r='10', fill=fillColor) 
     text = et.Element('text', x='28.80024', y='45.8372', fill=fillText, style='font-family:Sans;font-size:12px;text-anchor:middle;dominant-baseline:top') 
     text.text = Ambulances[j][1] 
     doc.append(text) 

#=============================================================================== 
# loop through the array 
#=============================================================================== 
    j = j + 1 

# ElementTree 1.2 doesn't write the SVG file header errata, so do that manually 
f = open('C:\sample.svg', 'w') 
f.write('<?xml version=\'1.0\' standalone=\'no\'?>\n') 
f.write('<!DOCTYPE svg PUBLIC \'-//W3C//DTD SVG 1.1//EN\'\n') 
f.write('\'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\'>\n') 
f.write(et.tostring(doc)) 
f.close() 
+0

您是否嘗試過加入你追加它之前新的行字符的文本? – AlexLordThorsen 2013-03-14 19:39:58

回答