python xml转换为字符串,插入postgres

python xml转换为字符串,插入postgres,python,xml,file,postgresql,parsing,Python,Xml,File,Postgresql,Parsing,这是我的密码: #!/usr/bin/python import psycopg2 import sys from lxml import etree def main(): #Define our connection string conn_string = ("host=host dbname=lal user=user password=pass") # get a connection, if a connect cannot be made an ex

这是我的密码:

#!/usr/bin/python

import psycopg2
import sys
from lxml import etree
def main():

    #Define our connection string
    conn_string = ("host=host dbname=lal user=user password=pass")


    # get a connection, if a connect cannot be made an exception will be raised here
    conn = psycopg2.connect(conn_string)

    # conn.cursor will return a cursor object
    cursor = conn.cursor()
    print "Connected!\n"

    # Open file


    parser = etree.parse("XML/epg.xml")
    for row in parser:
        print row

        postgres = ('INSERT INTO epg_live (channel_id, program, start, duration) VALUES (%s, %s, %s, %s)', (row, row, row, row))
        cursor.execute(parser,postgres)
        cursor.commit()
        print "Gotovo!"

if __name__ == "__main__":
    main()
您能帮我在posgresql中将XML文件解析为字符串并插入到表中吗。 运行脚本时,会出现如下错误:

File "./xml.py", line 32, in <module>
    main()
  File "./xml.py", line 22, in main
    parser = etree.parse("XML/epg.xml")
  File "lxml.etree.pyx", line 2953, in lxml.etree.parse (src/lxml/lxml.etree.c:56204)
  File "parser.pxi", line 1533, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:82287)
  File "parser.pxi", line 1562, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:82580)
  File "parser.pxi", line 1462, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:81619)
  File "parser.pxi", line 1002, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:78528)
  File "parser.pxi", line 569, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:74472)
  File "parser.pxi", line 650, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:75363)
  File "parser.pxi", line 590, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:74696)
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: epg line 2 and item, line 26, column 10
文件“/xml.py”,第32行,在
main()
文件“/xml.py”,第22行,主
parser=etree.parse(“XML/epg.XML”)
lxml.etree.parse(src/lxml/lxml.etree.c:56204)中第2953行的文件“lxml.etree.pyx”
文件“parser.pxi”,第1533行,在lxml.etree.\u parseDocument(src/lxml/lxml.etree.c:82287)中
文件“parser.pxi”,第1562行,位于lxml.etree.\u parseDocumentFromURL(src/lxml/lxml.etree.c:82580)
文件“parser.pxi”,第1462行,在lxml.etree.\u parseDocFromFile(src/lxml/lxml.etree.c:81619)中
文件“parser.pxi”,第1002行,在lxml.etree.\u BaseParser.\u parseDocFromFile(src/lxml/lxml.etree.c:78528)中
文件“parser.pxi”,第569行,在lxml.etree.\u ParserContext.\u handleParseResultDoc(src/lxml/lxml.etree.c:74472)中
文件“parser.pxi”,第650行,在lxml.etree.\u handleParseResult(src/lxml/lxml.etree.c:75363)中
lxml.etree中的文件“parser.pxi”,第590行。\u raiseParserError(src/lxml/lxml.etree.c:74696)
lxml.etree.xmlsyntaxer错误:开始和结束标记不匹配:epg第2行和第26行第10列的项目
我的XML很好,看起来像:

<item><program>        Program 3   
</program><start>            Start   20130918 15:00:00 
</start><duration>            Duration   04:30:00 
</duration><title>                  Title Nujna seja Odbora za finance in monetarno politiko   
</title></item>
程序3
开始时间:20130918 15:00:00
持续时间04:30:00
标题:货币政治中的金融

你能帮我提供一些python的解决方案吗,谢谢大家阅读这篇文章。

你可以将xml读入参数并发送到PostgreSQL,如下所示:

root = etree.parse("XML/epg.xml")
for i in root.findall("item"):
    p = [i.find(n).text for n in ("program", "start", "duration")]
    # now you get list with values of parameters

    postgres = ('INSERT INTO epg_live (program, start, duration) VALUES (%s, %s, %s)', p)
    cursor.execute(parser,postgres)
    cursor.commit()

不知道从何处获取
channel_id
parameter

在XML文件上运行
xmllint
命令以检查是否存在错误我可以毫无问题地解析XML示例,您必须检查XML,但如何将XML文件读入字符串并在postgresql上作为字符串发送?我得到以下错误:文件“/XML.py”,第22行p=[i.find(n).n在(“程序”、“开始”、“持续时间”、“标题”)中的文本]^indentation错误:未缩进与任何外部缩进级别不匹配检查程序中的缩进,您必须使用所有选项卡或所有空格sthx获取建议,我已全部检查,现在获得此:文件“/proba.py”,第24行p=[i.find(n).text^IndentationError:应该是一个缩进块,但我对python是新手,我想做得更好。Roman Pekar,运行代码时的解决方案:回溯(最近一次调用):File”。/proba.py,第22行,在root=etree.parse(“XML/epg.XML”)中,这是XML文件的问题吗?File”lxml.etree.pyx”,第2953行,在lxml.etree.parse(src/lxml/lxml.etree.c:56204)文件“parser.pxi”,第1533行,在lxml.etree.\u parseDocument(src/lxml/lxml.etree.c:82287)中