我已经将XML解析为列表,我想将其保存为txt文件
但是有个错误
如何将其保存到txt
我是python的初学者,所以我不知道怎么做
我的xml url是https://pvxml.map.naver.com/api/get?type=xml&pano_id=/c7VUP7/zsr2UT/De2VlQA==&rv=3
我想解析街道全景id
import os
import urllib.request as urllib from xml.dom
import minidom
with open ('list.txt','w') as f:
f.write('C:/Users/JYLEE/Desktop/python/exercise')
url = 'https://pvxml.map.naver.com/api/get?type=xml&pano_id=/c7VUP7/zsr2UT/De2VlQA==&rv=3'
dom = minidom.parse(urllib.urlopen(url))
link = dom.getElementsByTagName('street_panorama')
categories = [items.attributes['id'].value for items in link if items.attributes['id']]
f.write(categories)
错误消息是TypeError: write() argument must be str, not list
我不知道下一步该怎么办
您可以使用
str()
方法将列表(categories
)转换为字符串格式另外,当您试图
read
或write
一个文件时,尝试使用with
语句。如果您使用read
或write
命令,则应该在使用f.close()
命令后关闭文件如果您使用
with
命令,这会自动处理这是你的更正代码供你参考
正如消息所说,它需要传递一个字符串。只需执行
write(str(categories))
即可获得类似于['a', 'b', 'c']
或[1, 2, 3]
的表示。如果您希望每一个都位于新行或某种特定格式上,请循环categories
并逐项写入文件相关问题 更多 >
编程相关推荐