【问题标题】:python xml to string, insert into postgrespython xml到字符串,插入postgres
【发布时间】:2013-09-20 08:25:07
【问题描述】:

这是我的代码:

#!/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()

您能帮我将 XML 文件解析为字符串并插入到 posgresql 中的表中吗? 当我运行脚本时,我收到如下错误:

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 很好,看起来像:

<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>

你能帮我解决一下python的问题吗,谢谢大家阅读这篇文章。

【问题讨论】:

  • 在您的 XML 文件上运行 xmllint 命令以检查没有错误
  • 我可以毫无问题地解析你的xml示例,你必须检查你的xml
  • 但是如何将我的 xml 文件读入字符串并将其作为字符串发送到 postgresql 上?

标签: python xml file postgresql parsing


【解决方案1】:

您可以像这样将 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参数

【讨论】:

  • 我收到此错误:文件“./xml.py”,第 22 行 p = [i.find(n).text for n in ("program", "start", "duration" , "title")] ^ IndentationError: unindent 不匹配任何外部缩进级别
  • 检查程序中的缩进,你必须使用所有制表符或所有空格
  • 谢谢建议,我已经检查了所有内容,现在得到了这个:文件“./proba.py”,第 24 行 p = [i.find(n).text ^ IndentationError: expected an indented阻止
  • 抱歉,我是 python 新手,我想变得更好。
  • Roman Pekar,感谢我运行代码时的解决方案: Traceback(最近一次调用最后一次):文件“./proba.py”,第 22 行,在 root = etree.parse( "XML/epg.xml") XML 文件有问题吗?文件“lxml.etree.pyx”,第 2953 行,在 lxml.etree.parse (src/lxml/lxml.etree.c:56204) 文件“parser.pxi”,第 1533 行,在 lxml.etree._parseDocument (src/ lxml/lxml.etree.c:82287)
猜你喜欢
  • 2015-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 2012-02-27
  • 2016-06-23
相关资源
最近更新 更多