beautifulsou lxml解析器截断fi

2024-10-01 09:35:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用BeautifulSoup和python3.4来解析xml文件。我的代码如下:

from lxml import etree
from bs4 import BeautifulSoup
import psycopg2
import sys
import configparser
import re

with open(informationFileName) as infoFP:
        infoTableSoup = BeautifulSoup(infoFP, "xml")

infoTableSoupFileName.write(infoTableSoup.prettify())

informationFileName包含以下文本行:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><informationTable xmlns:ns1="http://www.sec.gov/edgar/common" xmlns:ns11="http://www.sec.gov/edgar/statecodes" xmlns="http://www.sec.gov/edgar/document/thirteenf/informationtable" xsi:schemaLocation="http://www.sec.gov/edgar/common eis_Common.xsd http://www.sec.gov/edgar/statecodes eis_stateCodes.xsd http://www.sec.gov/edgar/document/thirteenf/informationtable eis_13FDocument.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><infoTable><nameOfIssuer>1ST SOURCE CORP</nameOfIssuer><titleOfClass>COM</titleOfClass><cusip>336901103</cusip><value>166</value><shrsOrPrnAmt><sshPrnamt>3364</sshPrnamt><sshPrnamtType>SH</sshPrnamtType></shrsOrPrnAmt><investmentDiscretion>SOLE</investmentDiscretion><otherManager>1</otherManager><votingAuthority><Sole>3364</Sole><Shared>0</Shared><None>0</None></votingAuthority></infoTable></informationTable>
</XML>
</TEXT>
</DOCUMENT>
</SEC-DOCUMENT>

不幸的是,这篇文章都在一行。当我运行这个程序时,经过修饰的xml被截断如下:

<?xml version="1.0" encoding="utf-8"?>
<informationTable xmlns:ns1="htt"/>

但是,如果我在运行到下面之前格式化xml,那么它就被正确地美化了。你知道吗

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><informationTable xmlns:ns1="http://www.sec.gov/edgar/common" xmlns:ns11="http://www.sec.gov/edgar/statecodes" xmlns="http://www.sec.gov/edgar/document/thirteenf/informationtable" xsi:schemaLocation="http://www.sec.gov/edgar/common eis_Common.xsd http://www.sec.gov/edgar/statecodes eis_stateCodes.xsd http://www.sec.gov/edgar/document/thirteenf/informationtable eis_13FDocument.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <infoTable>
        <nameOfIssuer>1ST SOURCE CORP</nameOfIssuer>
        <titleOfClass>COM</titleOfClass>
        <cusip>336901103</cusip>
        <value>166</value>
        <shrsOrPrnAmt>
            <sshPrnamt>3364</sshPrnamt>
            <sshPrnamtType>SH</sshPrnamtType>
        </shrsOrPrnAmt>
        <investmentDiscretion>SOLE</investmentDiscretion>
        <otherManager>1</otherManager>
        <votingAuthority>
            <Sole>3364</Sole>
            <Shared>0</Shared>
            <None>0</None>
        </votingAuthority>
    </infoTable>
</informationTable> 
</XML> </TEXT> </DOCUMENT> </SEC-DOCUMENT>

为什么会这样?我什么都没做,只是加了些新词。我以为BeautifulSoup不应该关心文件的格式。你知道吗


Tags: importhttpwwwxmlseccommondocumentgov