2016-09-16 8 views
0

나는 파이썬 모듈 lxmlpsutil을 사용하여 XML 파일에 배치 할 시스템 메트릭을 기록하고 PHP로 구문 분석을 위해 원격 서버에 복사하여 사용자.lxml을 사용하여 시스템 성능 데이터로 xml 파일을 작성하는 Python

그러나 lxml은 XML의 여러 부분에 변수, 객체 등을 푸는 데 약간의 문제가 있습니다. 예를 들어

:

import psutil, os, time, sys, platform 
from lxml import etree 

# This creates <metrics> 
root = etree.Element('metrics') 

# and <basic>, to display basic information about the server 
child1 = etree.SubElement(root, 'basic') 

# First system/hostname, so we know what machine this is 
etree.SubElement(child1, "name").text = socket.gethostname() 

# Then boot time, to get the time the system was booted. 
etree.SubElement(child1, "boottime").text = psutil.boot_time() 

# and process count, see how many processes are running. 
etree.SubElement(child1, "proccount").text = len(psutil.pids()) 

라인 시스템의 호스트 이름의 작품을 얻을 수 있습니다. 다음 두 줄과 부팅 시간과 프로세스 수 오류가 밖으로 얻을 그러나

: 그래서

>>> etree.SubElement(child1, "boottime").text = psutil.boot_time() 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "lxml.etree.pyx", line 921, in lxml.etree._Element.text.__set__ (src/lxml/lxml.etree.c:41344) 
File "apihelpers.pxi", line 660, in lxml.etree._setNodeText (src/lxml/lxml.etree.c:18894) 
File "apihelpers.pxi", line 1333, in lxml.etree._utf8 (src/lxml/lxml.etree.c:24601) 
TypeError: Argument must be bytes or unicode, got 'float' 
>>> etree.SubElement(child1, "proccount").text = len(psutil.pids()) 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "lxml.etree.pyx", line 921, in lxml.etree._Element.text.__set__ (src/lxml/lxml.etree.c:41344) 
File "apihelpers.pxi", line 660, in lxml.etree._setNodeText (src/lxml/lxml.etree.c:18894) 
File "apihelpers.pxi", line 1333, in lxml.etree._utf8 (src/lxml/lxml.etree.c:24601) 
TypeError: Argument must be bytes or unicode, got 'int' 

을, 여기 내 XML 인쇄로의 모습입니다 : 그래서

>>> print(etree.tostring(root, pretty_print=True)) 
<metrics> 
    <basic> 
    <name>mercury</name> 
    <boottime/> 
    <proccount/> 
    </basic> 
</metrics> 

,이 어쨌든 내가 필요로하는 것처럼 float과 int를 XML 텍스트로 푸시? 아니면이 일을 완전히 잘못하고 있습니까?

도움을 주셔서 감사합니다.

답변

2

text 필드는 유니 코드 또는 str이며 다른 형식 (boot_timefloat이고 len()은 int)이 아닌 것으로 예상됩니다. 그러니 그냥 문자열로 변환되지 않은 문자열을 준수 요소 :

# First system/hostname, so we know what machine this is 
etree.SubElement(child1, "name").text = socket.gethostname() # nothing to do 

# Then boot time, to get the time the system was booted. 
etree.SubElement(child1, "boottime").text = str(psutil.boot_time()) 

# and process count, see how many processes are running. 
etree.SubElement(child1, "proccount").text = str(len(psutil.pids())) 

결과 :

내가 라이브러리가 isinstance(str,x) 테스트 또는 str 변환을 할 수있는 가정
b'<metrics>\n <basic>\n <name>JOTD64</name>\n <boottime>1473903558.0</boottime>\n <proccount>121</proccount>\n </basic>\n</metrics>\n' 

있지만 그런 식으로 설계되지 않았습니다 (선행 0, 잘림 소수 자릿수를 가진 부동 소수점을 표시하려면 어떻게해야합니까?). lib가 모든 것이 str 인 것으로 가정하면 더 빠릅니다. 대부분 시간입니다.

+0

음 .... 나는 상황을 너무 과소 평가하고있었습니다. 내 무능력에 대한 답변 주셔서 감사합니다! 너를위한 쿠키. – Jguy