Python:格式不正确的XML到数据框

时间:2018-12-09 23:43:17

标签: python xml pandas dataframe

我想解析以下xml示例:

<?xml version="1.0"?>
<all_news>
<source>Yahoo Business</source><url>http://us.rd.yahoo.com/dailynews/rss/business/*http://story.news.yahoo.com/news?tmpl=story2 u=/nm/20040814/bs_nm/column_stocks_week_dc</url><title>Wall St. Pullback Reflects Tech Blowout (Reuters)</title><image>none</image><category>Business</category><description>Reuters - Wall Street's long-playing drama,\"Waiting for Google," is about to reach its final act, but its\stock market debut is ending up as more of a nostalgia event\than the catalyst for a new era.</description><rank>5</rank><pubdate>0000-00-00 00:00:00</pubdate>
<source>Yahoo Business</source><url>http://us.rd.yahoo.com/dailynews/rss/business/*http://story.news.yahoo.com/news?tmpl=story2 u=/nm/20040814/bs_nm/markets_bears_dc</url><title>Wall St. Bears Claw Back Into the Black (Reuters)</title><image>none</image><category>Business</category><description>Reuters - Short-sellers, Wall Street's dwindling\band of ultra-cynics, are seeing green again.</description><rank>5</rank><pubdate>0000-00-00 00:00:00</pubdate>
<source>Yahoo Business</source><url>http://us.rd.yahoo.com/dailynews/rss/business/*http://story.news.yahoo.com/news?tmpl=story2 u=/nm/20040814/bs_nm/column_mergers_dc</url><title>Carlyle Looks Toward Commercial Aerospace (Reuters)</title><image>none</image><category>Business</category><description>Reuters - Private investment firm Carlyle Group,\which has a reputation for making well-timed and occasionally\controversial plays in the defense industry, has quietly placed\its bets on another part of the market.</description><rank>5</rank><pubdate>0000-00-00 00:00:00</pubdate>
<source>Yahoo Business</source><url>http://us.rd.yahoo.com/dailynews/rss/business/*http://story.news.yahoo.com/news?tmpl=story2 u=/nm/20040814/bs_nm/column_stocks_outlook_dc</url><title>Oil and Economy Cloud Stocks' Outlook (Reuters)</title><image>none</image><category>Business</category><description>Reuters - Soaring crude prices plus worries\about the economy and the outlook for earnings are expected to\hang over the stock market next week during the depth of the\summer doldrums.</description><rank>5</rank><pubdate>0000-00-00 00:00:00</pubdate>
<source>Yahoo Business</source><url>http://us.rd.yahoo.com/dailynews/rss/business/*http://story.news.yahoo.com/news?tmpl=story2 u=/nm/20040814/bs_nm/energy_iraq_dc</url><title>Iraq Halts Oil Exports from Main Southern Pipeline (Reuters)</title><image>none</image><category>Business</category><description>Reuters - Authorities have halted oil export\flows from the main pipeline in southern Iraq after\intelligence showed a rebel militia could strike\infrastructure, an oil official said on Saturday.</description><rank>5</rank><pubdate>0000-00-00 00:00:00</pubdate>
<source>Yahoo Business</source><url>http://us.rd.yahoo.com/dailynews/rss/business/*http://story.news.yahoo.com/news?tmpl=story2 u=/afp/20040815/bs_afp/us_economy</url><title>Oil prices soar to all-time record, posing new menace to US economy (AFP)</title><image>none</image><category>Business</category><description>AFP - Tearaway world oil prices, toppling records and straining wallets, present a new economic menace barely three months before the US presidential elections.</description><rank>5</rank><pubdate>0000-00-00 00:00:00</pubdate>
<source>Yahoo Business</source><url>http://us.rd.yahoo.com/dailynews/rss/business/*http://story.news.yahoo.com/news?tmpl=story2 u=/nm/bs_nm/markets_stocks_dc</url><title>Stocks End Up, But Near Year Lows (Reuters)</title><image>none</image><category>Business</category><description>Reuters - Stocks ended slightly higher on Friday\but stayed near lows for the year as oil prices surged past  #36;46\a barrel, offsetting a positive outlook from computer maker\Dell Inc. (DELL.O)</description><rank>5</rank><pubdate>0000-00-00 00:00:00</pubdate>
<source>Yahoo Business</source><url>http://us.rd.yahoo.com/dailynews/rss/business/*http://story.news.yahoo.com/news?tmpl=story2 u=/ap/20040812/ap_on_bi_ge/money_funds</url><title>Money Funds Fell in Latest Week (AP)</title><image>none</image><category>Business</category><description>AP - Assets of the nation's retail money market mutual funds fell by  #36;1.17 billion in the latest week to  #36;849.98 trillion, the Investment Company Institute said Thursday.</description><rank>5</rank><pubdate>0000-00-00 00:00:00</pubdate>
</all_news>
</xml>

请注意,每个数据行都在换行符上,并且没有封闭的xml元素。

我的幼稚解决方案读取每一行并分别解析每一行以获取数据。该解决方案基于:https://gokhanatil.com/2017/11/python-for-data-science-importing-xml-to-pandas-dataframe.html有更快的方法吗?

0 个答案:

没有答案