我使用 iterparse 来解析一个大的 xml 文件 (1,8 gb)。我将所有数据写入一个 csv 文件。t 我制作的脚本运行良好,但由于某种原因它随机跳过了几行。这是我的脚本:
import xml.etree.cElementTree as ET
import csv
xml_data_to_csv =open('Out2.csv','w', newline='', encoding='utf8')
Csv_writer=csv.writer(xml_data_to_csv, delimiter=';')
file_path = "Products_50_producten.xml"
context = ET.iterparse(file_path, events=("start", "end"))
EcommerceProductGuid = ""
ProductNumber = ""
Description = ""
ShopSalesPriceInc = ""
Barcode = ""
AvailabilityStatus = ""
Brand = ""
# turn it into an iterator
#context = iter(context)
product_tag = False
for event, elem in context:
tag = elem.tag
if event == 'start' :
if tag == "Product" :
product_tag = True
elif tag == 'EcommerceProductGuid' :
EcommerceProductGuid = elem.text
elif tag == 'ProductNumber' :
ProductNumber = elem.text
elif tag == 'Description' :
Description = elem.text
elif tag == 'SalesPriceInc' :
ShopSalesPriceInc = elem.text
elif tag == 'Barcode' :
Barcode = elem.text
elif tag == 'AvailabilityStatus' :
AvailabilityStatus = elem.text
elif tag == 'Brand' :
Brand = elem.text
if event == 'end' and tag =='Product' :
product_tag = False
List_nodes = []
List_nodes.append(EcommerceProductGuid)
List_nodes.append(ProductNumber)
List_nodes.append(Description)
List_nodes.append(ShopSalesPriceInc)
List_nodes.append(Barcode)
List_nodes.append(AvailabilityStatus)
List_nodes.append(Brand)
Csv_writer.writerow(List_nodes)
print(EcommerceProductGuid)
List_nodes.clear()
EcommerceProductGuid = ""
ProductNumber = ""
Description = ""
ShopSalesPriceInc = ""
Barcode = ""
AvailabilityStatus = ""
Brand = ""
elem.clear()
例如,如果我将“产品”复制 300 次,它会将 csv 文件中第 155 行的“EcommerceProductGuid”值留空。如果我复制 Product 400 次,它会在第 155、310 和 368 行留下一个空值。这怎么可能?
狐的传说
相关分类