Python XML到CSV合作项目

Python XML到CSV合作项目,python,xml,csv,Python,Xml,Csv,现在我在工作中发现了一个问题。 我需要来自的XML数据 我想解析XML并输出到CSV 我是python的新手 现在im测试编码解析PM10,如下所示 import xml.etree.ElementTree as ET import requests import os Air4thaiURL = 'http://air4thai.pcd.go.th/services/getAQI_XML.php?region=2' resp = requests.get(Air4thaiURL) msg =

现在我在工作中发现了一个问题。 我需要来自的XML数据 我想解析XML并输出到CSV

我是python的新手 现在im测试编码解析PM10,如下所示

import xml.etree.ElementTree as ET
import requests
import os
Air4thaiURL = 'http://air4thai.pcd.go.th/services/getAQI_XML.php?region=2'
resp = requests.get(Air4thaiURL)
msg = resp.content
tree = ET.fromstring(msg)
for station in tree.findall('.//station/LastUpdate'):
print ('{}'.format(
station.get('PM10')))   
但结果是这样的

import xml.etree.ElementTree as ET
import requests
import os
Air4thaiURL = 'http://air4thai.pcd.go.th/services/getAQI_XML.php?region=2'
resp = requests.get(Air4thaiURL)
msg = resp.content
tree = ET.fromstring(msg)
for station in tree.findall('.//station/LastUpdate'):
print ('{}'.format(
station.get('PM10')))   
没有 没有 没有 没有 没有 没有 没有 没有 没有 没有 没有 没有 没有 没有 没有 没有 没有

所以,它必须显示在Value/Im中,并尝试键入:s it's result

Traceback (most recent call last):
  File "C:\Users\Gistda59\Desktop\Coop - Python Script\pm10_XML.py", line 11, in <module>
    station.get('PM10')))
TypeError: non-empty format string passed to object.__format__
回溯(最近一次呼叫最后一次):
文件“C:\Users\Gistda59\Desktop\Coop-Python Script\pm10_XML.py”,第11行,在
加油站(PM10)
TypeError:传递给对象的非空格式字符串。\uuu格式__
如何解决这个问题,并建议我从这个URL将XML解析为CSV
非常感谢您帮助解决我的问题。

这就是您想要的:

import xml.etree.ElementTree as ET
import requests
import os
Air4thaiURL = 'http://air4thai.pcd.go.th/services/getAQI_XML.php?region=2'
resp = requests.get(Air4thaiURL)
msg = resp.content
tree = ET.fromstring(msg)
for station in tree.findall('.//station/LastUpdate'):
    if station.tag == 'LastUpdate':
        for item in station:
            if item.tag == 'PM10':
                print(item.attrib)