Pyt*_*ang 4 html javascript python xml pandas
我对 HTML 和 javascript 非常陌生。遇到了很多关于我的问题的问题,在努力寻找解决方案之后,我发布了这个问题。
问题陈述:
我有一个 xml,我试图将其转换为 HTML,以便我可以以表格格式在 Web 浏览器上显示它。
<?xml version="1.0" encoding="UTF-8"?>
<chapter name="ndlkjfidm" date="dfhkryi">
<edge name="nnn" P="ffgnp" V="0.825" T="125c">
<seen name="seen1">
</seen>
<seen name="ABB">
<mob name="adas_jk3" type="entry">
<nod name="VSS" voltage="0.000000" vector="!ENXB" active_input="NA" active_ouput="ENX">
<temp name="ADS_DEFAULT_temp_LOW">
<raw nod="VBP" alt="7.05537e-15" jus="74.4619" />
<raw nod="VDDC" alt="4.63027e-10" jus="115.178" />
<raw nod="VDDP" alt="6.75316e-10" jus="115.178" />
<raw nod="VSS" alt="5.04568e-14" jus="9.63935" />
<raw nod="VBN" alt="1.21047e-14" jus="192.973" />
<raw nod="VBP" trip="4.58141e-12" />
<raw nod="VDDC" trip="5.19549e-09" />
<raw nod="VDDP" trip="5.49458e-08" />
<raw nod="VSS" trip="6.00563e-08" />
<raw nod="VBN" trip="8.94924e-11" />
</temp>
</nod>
<nod name="VSS" voltage="0.000000" vector="ENXB" active_input="NA" active_ouput="ENX">
<temp name="ADS_DEFAULT_temp_HIGH">
<raw nod="VBP" alt="7.05537e-15" jus="74.4644" />
<raw nod="VDDC" alt="1.52578e-14" jus="311.073" />
<raw nod="VDDP" alt="1.00188e-14" jus="521.709" />
<raw nod="VSS" alt="4.03483e-14" jus="11.1118" />
<raw nod="VBN" alt="1.21047e-14" jus="192.975" />
<raw nod="VBP" trip="4.58141e-12" />
<raw nod="VDDC" trip="1.29302e-12" />
<raw nod="VDDP" trip="4.92723e-08" />
<raw nod="VSS" trip="4.91887e-08" />
<raw nod="VBN" trip="8.95356e-11" />
</temp>
</nod>
</mob>
</seen>
</edge>
</chapter>
Run Code Online (Sandbox Code Playgroud)
以下是我尝试过的链接。
https://www.w3schools.com/xml/ajax_applications.asp
循环孔:
我无法安装任何东西(sudo apt install apache2 等..)或任何软件(xammp 等),因此 javascript 不显示表格。
也尝试过 pandas,但不知道如何在网络浏览器上显示它,而且 xml 也非常大(〜1GB)
有人可以建议我如何使用任何语言组合来完成此任务吗?
这应该使用 pandas 解决您的问题(按照要求):
import pandas as pd
xml_data = '''<?xml version="1.0" encoding="UTF-8"?>
<chapter name="ndlkjfidm" date="dfhkryi">
<edge name="nnn" P="ffgnp" V="0.825" T="125c">
<seen name="seen1">
</seen>
<seen name="ABB">
<mob name="adas_jk3" type="entry">
<nod name="VSS" voltage="0.000000" vector="!ENXB" active_input="NA" active_ouput="ENX">
<temp name="ADS_DEFAULT_temp_LOW">
<raw nod="VBP" alt="7.05537e-15" jus="74.4619" />
<raw nod="VDDC" alt="4.63027e-10" jus="115.178" />
<raw nod="VDDP" alt="6.75316e-10" jus="115.178" />
<raw nod="VSS" alt="5.04568e-14" jus="9.63935" />
<raw nod="VBN" alt="1.21047e-14" jus="192.973" />
<raw nod="VBP" trip="4.58141e-12" />
<raw nod="VDDC" trip="5.19549e-09" />
<raw nod="VDDP" trip="5.49458e-08" />
<raw nod="VSS" trip="6.00563e-08" />
<raw nod="VBN" trip="8.94924e-11" />
</temp>
</nod>
<nod name="VSS" voltage="0.000000" vector="ENXB" active_input="NA" active_ouput="ENX">
<temp name="ADS_DEFAULT_temp_HIGH">
<raw nod="VBP" alt="7.05537e-15" jus="74.4644" />
<raw nod="VDDC" alt="1.52578e-14" jus="311.073" />
<raw nod="VDDP" alt="1.00188e-14" jus="521.709" />
<raw nod="VSS" alt="4.03483e-14" jus="11.1118" />
<raw nod="VBN" alt="1.21047e-14" jus="192.975" />
<raw nod="VBP" trip="4.58141e-12" />
<raw nod="VDDC" trip="1.29302e-12" />
<raw nod="VDDP" trip="4.92723e-08" />
<raw nod="VSS" trip="4.91887e-08" />
<raw nod="VBN" trip="8.95356e-11" />
</temp>
</nod>
</mob>
</seen>
</edge>
</chapter>
'''
Run Code Online (Sandbox Code Playgroud)
读取 xml 的一种选择是:
df = pd.read_xml(xml_data)
# df
html = df.to_html()
print(html)
Run Code Online (Sandbox Code Playgroud)
结果:
import pandas as pd
xml_data = '''<?xml version="1.0" encoding="UTF-8"?>
<chapter name="ndlkjfidm" date="dfhkryi">
<edge name="nnn" P="ffgnp" V="0.825" T="125c">
<seen name="seen1">
</seen>
<seen name="ABB">
<mob name="adas_jk3" type="entry">
<nod name="VSS" voltage="0.000000" vector="!ENXB" active_input="NA" active_ouput="ENX">
<temp name="ADS_DEFAULT_temp_LOW">
<raw nod="VBP" alt="7.05537e-15" jus="74.4619" />
<raw nod="VDDC" alt="4.63027e-10" jus="115.178" />
<raw nod="VDDP" alt="6.75316e-10" jus="115.178" />
<raw nod="VSS" alt="5.04568e-14" jus="9.63935" />
<raw nod="VBN" alt="1.21047e-14" jus="192.973" />
<raw nod="VBP" trip="4.58141e-12" />
<raw nod="VDDC" trip="5.19549e-09" />
<raw nod="VDDP" trip="5.49458e-08" />
<raw nod="VSS" trip="6.00563e-08" />
<raw nod="VBN" trip="8.94924e-11" />
</temp>
</nod>
<nod name="VSS" voltage="0.000000" vector="ENXB" active_input="NA" active_ouput="ENX">
<temp name="ADS_DEFAULT_temp_HIGH">
<raw nod="VBP" alt="7.05537e-15" jus="74.4644" />
<raw nod="VDDC" alt="1.52578e-14" jus="311.073" />
<raw nod="VDDP" alt="1.00188e-14" jus="521.709" />
<raw nod="VSS" alt="4.03483e-14" jus="11.1118" />
<raw nod="VBN" alt="1.21047e-14" jus="192.975" />
<raw nod="VBP" trip="4.58141e-12" />
<raw nod="VDDC" trip="1.29302e-12" />
<raw nod="VDDP" trip="4.92723e-08" />
<raw nod="VSS" trip="4.91887e-08" />
<raw nod="VBN" trip="8.95356e-11" />
</temp>
</nod>
</mob>
</seen>
</edge>
</chapter>
'''
Run Code Online (Sandbox Code Playgroud)
当然,您可以深入查看该 xml:
df = pd.read_xml(xml_data, xpath=".//nod")
# df
html = df.to_html()
print(html)
Run Code Online (Sandbox Code Playgroud)
这将导致:
df = pd.read_xml(xml_data)
# df
html = df.to_html()
print(html)
Run Code Online (Sandbox Code Playgroud)
甚至:
df = pd.read_xml(xml_data, xpath=".//raw")
# df
html = df.to_html()
print(html)
Run Code Online (Sandbox Code Playgroud)
返回:
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>name</th>
<th>P</th>
<th>V</th>
<th>T</th>
<th>seen</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>nnn</td>
<td>ffgnp</td>
<td>0.825</td>
<td>125c</td>
<td>NaN</td>
</tr>
</tbody>
</table>Run Code Online (Sandbox Code Playgroud)
以下 pandas 文档可能会有所帮助:
https://pandas.pydata.org/docs/dev/reference/api/pandas.read_xml.html
和
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_html.html