尝试将ElementTree写入XML fi时,“TypeError:write()参数必须是str,而不是字节”

2024-06-06 14:08:13 发布

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

我试图用^{}方法将一些XML作为字节字符串(即Zillow API的响应)写入XML文件。下面是我要做的一个例子:

from xml.etree import ElementTree

content = b'<?xml version="1.0" encoding="utf-8"?><Comps:comps xsi:schemaLocation="http://www.zillow.com/static/xsd/Comps.xsd https://www.zillowstatic.com/vstatic/b20c067/static/xsd/Comps.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:Comps="http://www.zillow.com/static/xsd/Comps.xsd"><request><zpid>48789177</zpid><count>1</count></request><message><text>Request successfully processed</text><code>0</code></message><response><properties><principal><zpid>48789177</zpid><links><homedetails>https://www.zillow.com/homedetails/1614-E-Columbia-St-Seattle-WA-98122/48789177_zpid/</homedetails><graphsanddata>http://www.zillow.com/homedetails/1614-E-Columbia-St-Seattle-WA-98122/48789177_zpid/#charts-and-data</graphsanddata><mapthishome>http://www.zillow.com/homes/48789177_zpid/</mapthishome><comparables>http://www.zillow.com/homes/comps/48789177_zpid/</comparables></links><address><street>1614 E Columbia St</street><zipcode>98122</zipcode><city>SEATTLE</city><state>WA</state><latitude>47.609388</latitude><longitude>-122.310834</longitude></address><taxAssessmentYear>2016</taxAssessmentYear><taxAssessment>528000.0</taxAssessment><yearBuilt>1907</yearBuilt><lotSizeSqFt>2613</lotSizeSqFt><finishedSqFt>2430</finishedSqFt><bathrooms>2.0</bathrooms><bedrooms>4</bedrooms><zestimate><amount currency="USD">900186</amount><last-updated>04/13/2018</last-updated><oneWeekChange deprecated="true"></oneWeekChange><valueChange duration="30" currency="USD">112285</valueChange><valuationRange><low currency="USD">783162</low><high currency="USD">1053218</high></valuationRange><percentile>90</percentile></zestimate><localRealEstate><region name="Minor" id="271921" type="neighborhood"><zindexValue>588,800</zindexValue><links><overview>http://www.zillow.com/local-info/WA-Seattle/Minor/r_271921/</overview><forSaleByOwner>http://www.zillow.com/minor-seattle-wa/fsbo/</forSaleByOwner><forSale>http://www.zillow.com/minor-seattle-wa/</forSale></links></region></localRealEstate></principal><comparables><comp score="7.0"><zpid>89822967</zpid><links><homedetails>https://www.zillow.com/homedetails/1711-E-Fir-St-Seattle-WA-98122/89822967_zpid/</homedetails><graphsanddata>http://www.zillow.com/homedetails/1711-E-Fir-St-Seattle-WA-98122/89822967_zpid/#charts-and-data</graphsanddata><mapthishome>http://www.zillow.com/homes/89822967_zpid/</mapthishome><comparables>http://www.zillow.com/homes/comps/89822967_zpid/</comparables></links><address><street>1711 E Fir St</street><zipcode>98122</zipcode><city>SEATTLE</city><state>WA</state><latitude>47.602463</latitude><longitude>-122.309743</longitude></address><taxAssessmentYear>2016</taxAssessmentYear><taxAssessment>513000.0</taxAssessment><yearBuilt>2008</yearBuilt><lotSizeSqFt>1306</lotSizeSqFt><finishedSqFt>1490</finishedSqFt><bathrooms>3.0</bathrooms><bedrooms>3</bedrooms><lastSoldDate>03/15/2018</lastSoldDate><lastSoldPrice currency="USD">783000</lastSoldPrice><zestimate><amount currency="USD">791686</amount><last-updated>04/13/2018</last-updated><oneWeekChange deprecated="true"></oneWeekChange><valueChange duration="30" currency="USD">-8370</valueChange><valuationRange><low currency="USD">752102</low><high currency="USD">831270</high></valuationRange><percentile>87</percentile></zestimate><localRealEstate><region name="Minor" id="271921" type="neighborhood"><zindexValue>588,800</zindexValue><links><overview>http://www.zillow.com/local-info/WA-Seattle/Minor/r_271921/</overview><forSaleByOwner>http://www.zillow.com/minor-seattle-wa/fsbo/</forSaleByOwner><forSale>http://www.zillow.com/minor-seattle-wa/</forSale></links></region></localRealEstate></comp></comparables></properties></response></Comps:comps><!-- H:042  T:99ms  S:2011  R:Sun Apr 15 09:01:05 PDT 2018  B:5.0.53029.1-hotfix_ADAT-2519.69881bc~hotfix-platform-for-2018-04-11.319b04e -->'
root = ElementTree.fromstring(content)
tree = ElementTree.ElementTree(root)

with open(f"comparables.xml", 'w+') as fp:
    tree.write(fp)

但是,如果我运行这个(在iPython中),我得到一个TypeError

^{pr2}$

我传递的不是文档中指定的“文件对象已打开以供写入”吗?在

我尝试过遵循ElementTree TypeError "write() argument must be str, not bytes" in Python3中的解决方案,但是tostring方法似乎不再包含在xml.etree模块中。在


Tags: comhttpwwwlinkscurrencyusdxsdst