나는 파이썬 모듈 lxml
과 psutil
을 사용하여 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 텍스트로 푸시? 아니면이 일을 완전히 잘못하고 있습니까?
도움을 주셔서 감사합니다.
음 .... 나는 상황을 너무 과소 평가하고있었습니다. 내 무능력에 대한 답변 주셔서 감사합니다! 너를위한 쿠키. – Jguy