Python reads XML, CSV files

  1. Read the xml file
    my_xml.xml
<?xml version="1.0" encoding="UTF-8"?>
<calc>
    <num>
        <num1>23</num1>
        <num2>2</num2>
    </num>
    <operator>
        <opt1>"+"</opt1>
        <opt2>"-"</opt2>
    </operator>
</calc>

python read file code

from xml.dom import minidom

xml_content = minidom.parse("my_xml.xml")
print(xml_content)
num1 = xml_content.getElementsByTagName("num1")[0].firstChild.data
print(num1)
num2 = xml_content.getElementsByTagName("num2")[0].firstChild.data
print(num2)
opt = xml_content.getElementsByTagName("opt1")[0].firstChild.data
print(opt)

Output:

"D:\python 3.8.2 install\python.exe" "D:/python programe files/Selenium_tests/readXml.py"
<xml.dom.minidom.Document object at 0x037C18F8>
23
2
"+"

进程已结束,退出代码0
  1. Read CSV file

1) Create a csv file

Open the wps worksheet -> save the file as -> select "CSV comma separated.csv" in the file type

Insert picture description here

test.csv

A,B,C
3,"""X""","""X"""
4,"""/""","""/"""
"""+""",0,0
"""-""",1,1
"""X""",2,2
"""/""",3,3
,4,4

Python read CSV file code

import csv

csv_content = csv.reader(open("test.csv"))
print(csv_content)
print(type(csv_content))
for line in csv_content:
    print(line)

Guess you like

Origin blog.csdn.net/KathyLJQ/article/details/106963361