【发布时间】:2018-01-17 22:32:45
【问题描述】:
我正在处理一个可用的 xml 文件here。
我想将 LON, LAT, PGA, PGV, MMI, PSA03, PSA10, PSA30, STDPGA, URAT and SVEL 解析并加载为 CSV 文件的标题。
grid_data 元素以空格分隔符的方式包含所有这些标头的所有值。
我正在寻找如下csv file output:
LON LAT PGA PGV MMI PSA03 PSA10 PSA30 STDPGA URAT SVEL
-99.6833 38.2891 0.04 0.04 2.04 0.09 0.02 0 0.65 1 363.294
-99.6666 38.2891 0.04 0.04 2.06 0.09 0.02 0 0.65 1 342.531
-99.6500 38.2891 0.04 0.04 2.11 0.1 0.02 0 0.65 1 303.783
-99.6333 38.2891 0.04 0.04 2.08 0.09 0.02 0 0.65 1 334.629
-99.6166 38.2891 0.04 0.05 2.15 0.09 0.02 0 0.65 1 279.535
-99.6000 38.2891 0.04 0.04 2.08 0.09 0.02 0 0.65 1 326.391
-99.5833 38.2891 0.04 0.04 2.02 0.08 0.02 0 0.65 1 390.897
-99.5666 38.2891 0.04 0.04 2.08 0.09 0.02 0 0.65 1 346.033
稍后,我会使用 pandas for python 来查找最大 PGV 值并进行 GIS 分析。
到目前为止,这是我拥有的代码:
import sys
import traceback
from xml.dom import minidom
import warnings
warnings.filterwarnings("ignore")
try:
print "*"*20 + " The Beginning " + "*"*20
xml_file_location = r"C:\Users\*****\Downloads\Grids\us2000a3y4_grid.xml"
xmldoc = minidom.parse(xml_file_location)
itemlist = xmldoc.getElementsByTagName('grid_field')
for item in itemlist:
print (item.attributes['name'].value)
Catch all exception and print to the screen
except:
e = sys.exc_info()[0]
print( "Error: %s\n\n" % e )
#Closing script
finally:
print "*"*20 + " The End " + "*"*20
【问题讨论】:
-
你尝试过什么吗?
-
编辑了问题并粘贴了我到目前为止的代码。
-
有什么错误吗?输出是什么?
-
******************** 开始 ******************** LON LAT PGA PGV MMI PSA03 PSA10 PSA30 STDPGA URAT SVEL ******************** 结束 ***************** ***
-
我不知道如何输出到 csv 文件并解析空格分隔符格式的 grid_data
标签: python pandas csv xml-parsing