我正在尝试读取,存储在数据帧中并将一些.csv文件写入XML文件。 XML文件具有一个架构,我曾经使用GenerateDS在Python中生成数据结构。我的问题是我不知道如何将子元素添加到父元素或根元素。
到目前为止,我已经能够生成具有其属性的根元素。
...
for {
idOpt <- q.update.withGeneratedKeys[Long]("id").compile.last
_ <- idOpt.fold(0.pure[ConnectionIO])(_ => inserts.reduce(_ ++ _).update.run)
} yield idOpt
模式(其中的一部分)如下:
import xmlschema
import csv
from pprint import pprint
import sys
sys.path.insert(0, r"C:\Users\Blabla\Desktop\py scripts\csvtoxml")
import rzbr
import glob
import pandas as pd
import ntpath
import numpy as np
import lxml
ntpath.basename("a/b/c")
def path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
Filenames=[fname for fname in glob.glob(r"C:\Users\Blabla\Desktop\py scripts\csvtoxml\Example\Data_CSV\*.csv")]
data_frames = {}
for filename in Filenames:
df = pd.read_csv(filename, sep=',')
data_frames[path_leaf(filename.partition(".")[0])] = df
Application = data_frames['Application'].astype(object).replace(np.nan, '')
ApplicantActivityStructure = data_frames['ApplicantActivityStructure'].astype(object).replace(np.nan, '')
ApplicantDetails = data_frames['ApplicantDetails'].astype(object).replace(np.nan, '')
ApplicantFinancials = data_frames['ApplicantFinancials'].astype(object).replace(np.nan, '')
AssociatedApplicant = data_frames['AssociatedApplicant'].astype(object).replace(np.nan, '')
CashFlowFX = data_frames['CashFlowFX'].astype(object).replace(np.nan, '')
Collateral = data_frames['Collateral'].astype(object).replace(np.nan, '')
CollateralAllocations = data_frames['CollateralAllocations'].astype(object).replace(np.nan, '')
ConsolidatedAccountData = data_frames['ConsolidatedAccountData'].astype(object).replace(np.nan, '')
CovenantsFulfillment = data_frames['CovenantsFulfillment'].astype(object).replace(np.nan, '')
CRCExposures = data_frames['CRCExposures'].astype(object).replace(np.nan, '')
CRCOverDueHistory = data_frames['CRCOverDueHistory'].astype(object).replace(np.nan, '')
ExchangeRates = data_frames['ExchangeRates'].astype(object).replace(np.nan, '')
FactoringPartnerData = data_frames['FactoringPartnerData'].astype(object).replace(np.nan, '')
PaymentInstruments = data_frames['PaymentInstruments'].astype(object).replace(np.nan, '')
RbroFacility = data_frames['RbroFacility'].astype(object).replace(np.nan, '')
RelatedParty = data_frames['RelatedParty'].astype(object).replace(np.nan, '')
RequestDetails = data_frames['RequestDetails'].astype(object).replace(np.nan, '')
for row in Application.itertuples():
app = rzbr.Application(
ApplicationDate=row.ApplicationDate,SWDecision=row.SWDecision, DeliveryOptionCode=row.DeliveryOptionCode,
ProcessingRequestType=row.ProcessingRequestType, ResubmissionFlag=row.ResubmissionFlag,
ApplicationCrossReferenceId=row.ApplicationCrossReferenceId
)
app.export(sys.stdout, 0)
输出应如下所示:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Application">
<xs:complexType>
<xs:sequence>
<xs:element name="CreditRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="ProductRequestList">
<xs:complexType>
<xs:sequence>
<xs:element name="ProductRequest">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="ProductCategory"/>
<xs:attribute type="xs:string" name="ProductCode"/>
<xs:attribute type="xs:string" name="DMFunction"/>
<xs:attribute type="xs:string" name="DMProduct"/>
<xs:attribute type="xs:byte" name="Priority"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:schema>
我的实际输出是:
<?xml version="1.0" encoding="UTF-8"?>
<Application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationDate="2017-07-01" SWDecision="false" DeliveryOptionCode="RZB" ProcessingRequestType="DA" ResubmissionFlag="false" ApplicationCrossReferenceId="111.0">
<CreditRequest>
<ProductRequestList>
<ProductRequest ProductCategory="Revolving" ProductCode="Revolving" DMFunction="" DMProduct="" Priority="0"> </ProductRequest>
</ProductRequestList>
</CreditRequest>
<Application ApplicationDate="2017-07-01" SWDecision="False" DeliveryOptionCode="RZB" ProcessingRequestType="DA" ResubmissionFlag="False" ApplicationCrossReferenceId="111."/>
是具有GenerateDS所生成的类和函数的模块,用于基于提供的模式的数据结构。