python´¦ÀíXML
»ù±¾É϶¼ÊÇʹÓÃpythonÀ´½âÎöxmlÎļþµÄ¡£
±ÈÈçÎÒÒª½«ÄÚÈÝΪ
<?xml version="1.0" encoding="utf-8"?>
<root>
<book isbn="34909023">
<author>
dikatour
</author>
</book>
</root>
дÈëµ½xmlstuff.xmlÎļþÖÐÈ¥¡£
ÆäʵҲºÜ¼òµ¥£¬»ù±¾ÔÀíÈçÏ£º
ÎÒʹÓÃxmlµÄDOM·½Ê½£¬ÏÈÔÚÄÚ´æÖд´½¨Ò»¸ö¿ÕµÄDOMÊ÷£¬È»ºó²»¶ÏÔö¼ÓÎÒÒªµÄ½Úµã£¬×îºóÐγÉÎÒÏëÒªµÄDOM£¬×îºóÊä³öµ½ÎļþÖÐÈ¥¡£
1.ÎÒʹÓÃxml.dom.minidomÕâ¸ömoduleÀ´´´½¨xmlÎļþ
from xml.dom import minidom
2. ÿ¸öxmlÎļþ¶¼ÊÇÒ»¸öDocument¶ÔÏó,´ú±í×ÅÄÚ´æÖеÄDOMÊ÷
doc = minidom.Document()
3.ÓÐÁ˿յÄDOMÊ÷ºó£¬ÎÒÃÇÔÚÉÏÃæÌí¼Ó¸ù½Úµã
rootNode = doc.createElement("root")
doc.appendChild(rootNode) #×¢ÒâpythonµÄlibrary referenceÀï˵,createElementºó²¢Ã»Óн«½Úµã¶ÔÏó¼Óµ½DOMÊ÷ÉÏ£¬ÐèÒª×Ô¼ºÊÖ¹¤¼ÓÉÏ
4.´´½¨ÆäËüµÄ½Úµã
5.Êä³öµ½xmlÎļþÖÐÈ¥
doc.writexml(f, "\t", "\t", "\n", "utf-8") #µÚÒ»¸ö²ÎÊýf¾ÍÊÇÄãµÄÄ¿±êÎļþ¶ÔÏ󣬵ڶþ¸ö²ÎÊýºÃÏñÊÇ<?xml>ºÍÏÂÃæÒ»¸ö¸ù½ÚµãµÄËõ½øÅÅÁиñʽ,
µÚÈý¸ö²ÎÊýºÃÏñÊÇÆäËû½ÚµãÓë×Ó½ÚµãµÄËõ½øÅÅÁиñʽ£¬µÚËĸö²ÎÊýÖÆ¶¨ÁË»»Ðеĸñʽ(Èç¹ûÄãÌîÈë" "£¬ÄǾͲ»»»ÐÐÁË£¬ËùÓеÄxml¶¼ËõÔÚÁËÒ»ÐÐÉÏÃæ :) )
£¬µÚÎå¸ö²ÎÊýÖÆ¶¨ÁËxmlÄÚÈݵıàÂë¡£³ýÁ˵ÚÒ»¸ö²ÎÊýÊDZØÐëµÄ£¬ÆäËû²ÎÊý¶¼ÊÇ¿ÉÑ¡ÔñµÄ¡£
×îÖÕ´úÂëÈçÏÂ(Õâ¸ö³ÌÐòûʲô¼ÛÖµ£¬Ö»ÊÇÓÃÀ´²âÊÔÑéÖ¤×Ô¼ºµÄÏë·¨,Äã¸ü¿ÉÄܶ¨ÒåÒ»¸ö¼òµ¥Àà»òº¯Êý£¬½«ÄãµÄÊý¾Ý½á¹¹ÐòÁл¯µ½xmlÎļþÖÐ)£º
from xml.dom import minidom
import traceback
try:
f = open("xmlstuff.xml", "w")
try:
doc = minidom.Document()
rootNode = doc.createElement("root")
Ïà¹ØÎĵµ£º
ÕÛÌÚ¼ÆËã»ú£¬±»¼ÆËã»úÕÛÌÚ£¬Ç°Ç°ºóºóÒ²ÓÐÊ®¼¸Äê¡£
×î¿É±¯µÄÊǾÓȻһÎÞËù³É£¬ËäÈ»½ö½ö³öÓÚÐËȤ£¬²¢ÎÞ¹¤×÷֮ѹÁ¦£¬µ«ÊÇÕâÖÖ´ìÕÛ¸ÐȷʵÊÇʵ´òʵµÄ¡£
ÒѾ¼Ç²»ÆðÊÇÄÄÒ»ÌìÁË£¬ºÃÏñͻȻ¾ÍÖªµÀÁËpythonµÄ´æÔÚ£¬´Ó×î³õµÄ²»¾Ò⣬µ½Ô½À´Ô½Ï²°®£¬ÎÒÒ»²½²½µÄÏòpython¿¿½ü¡£
ÎÒϲ»¶Python£¬ÕâÊÇÒ»¸öÊÂʵ£¬ÎÒ׫´ ......
1.open
ʹÓÃopen´ò¿ªÎļþºóÒ»¶¨Òª¼ÇµÃµ÷ÓÃÎļþ¶ÔÏóµÄclose()·½·¨¡£±ÈÈç¿ÉÒÔÓÃtry/finallyÓï¾äÀ´È·±£×îºóÄܹرÕÎļþ¡£
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
×¢£º²»Ä ......
ǰһƪÎÄÕÂдµÄÔÚAPACHE°²×°MOD_PYTHONµÄ¾¹ý,Æäʵͦ¼òµ¥,¾ÍÊǰ汾²»¼æÈݵÄÎÊÌâ.Õâ´ÎÎÒ´ó¸Å˵ϲ¿ÊðDJANGOµÄ¹ý³Ì.
ÏÈÐÞ¸ÄAPACHEÅäÖÃÎļþ,ʹÆä¼ÓÔØmod_pythonÄ£¿é
LoadModule python_module libexec/mod_python.so
ÔËÐÐÃüÁî²é¿´
bin/httpd -M¿ÉÒÔ¿´µ½
python_module (shared)
Syntax OK
˵Ã÷apacheÒѾ³É¹¦¼Ó ......
С°×¶ÔÉϽڿÎÀïµÄ“СÂéȸ”ÐËȤ°»È»£¬ÕûÌì¾ÍÔÚËÞÉáµ·¹Ä¡£Õâ²»£¬¼¸ÌìÏÂÀ´ËûÒѾ¹¥ÏÂÊ®¼¸¸öÍøÕ¾ÁË¡£
С²Ëȴϲ»¶Ò»¸öÈË×øÔÚµçÄÔÅԱߣ¬Öå×Åüͷ£¬ÐÀÉÍ×ÅÕâÒ»ÐÐÐÐ×Ô¼º¿´²»¶®µÄ´úÂë¡£“ÕâЩ×Ö·ûºÃ¹ÖÒìѽ£¡”С²ËÍá×ÅÍ·£¬“ËüΪɶ½ÐÕýÔò±í´ïÊ½ÄØ£¿”
°Ù¶Èһϣ¬ÖÕÓÚÖªµÀ¡£Ð¡² ......
µ±ÎÒÃÇÕâÑù½¨Á¢Îļþʱ
f =
file('x1.txt', 'w')
f.write(u'ÖÐÎÄ')
f.colse()
Ö±
½Ó½á¹ûÓ¦¸ÃÊÇÀàËÆ
f.write(u'ÖÐÎÄ')
UnicodeEncodeError: 'ascii'
codec can't encode characters in position 0-16: ordinal not in
range(128)
ÒªÖ±½Óд utf-8 ÎļþÔõô°ìÄØ?
import codecs
f = codecs. ......