我已经看到了一些关于如何将csv(或SQL表格)转换为XML的答案,但是我还没有看到包含层级和isn'明显复杂。我需要将csv文件映射到预先存在的XML格式,以使用Python将其提供给API。我已经可以向网站发送一个有效的XML,但是我首先会遇到将csv转换为XML的问题。使用Python将层级转换为带有层级的XML通过API提供
我的CSV格式如下:
OrganizationName,OrdNum,OrdType,OrderTMSStatus,FreightTerms,IsPrePayment,ScheduledEarlyPickup,ScheduledEarlyDelivery,WeightValue,uom,WeightBase,uom2,WeightValue3,uom4,WeightBase5,uom6,VolumeValue,uom7,VolumeBase,uom8,VolumeValue9,uom10,VolumeBase11,uom12,TotalPieceCount,TotalHandlingUnitCount,IsInPlanning,AreTotalsOverridden,CurrencyValue,uom13,CurrencyBase,uom14,IsHot,IsHazmat,BillingStatus,IntegrationStatus,OriginLocNum,OrganizationName15,TradingPartnerNum,TradingPartnerType,LocNum,LocationType,IsActive,IsBillTo,IsRemitTo,IsCorporate,AddrName,Addr1,CityName,StateCode,CountryISO2,PostalCode,CalendarName,CalendarAppointmentName,AllowsHazmat,IsDeliveryAptRequired,IsPickupAptRequired,DestinationLocNum,OrganizationName16,TradingPartnerNum17,TradingPartnerType18,LocNum19,LocationType20,IsActive21,IsBillTo22,IsRemitTo23,IsCorporate24,AddrName25,Addr126,CityName27,StateCode28,CountryISO229,PostalCode30,CalendarName31,CalendarAppointmentName32,AllowsHazmat33,IsDeliveryAptRequired34,IsPickupAptRequired35,OrganizationName36,TradingPartnerNum37,TradingPartnerName,TradingPartnerType38,IsActive39,OrdLineNum,WeightValue40,uom41,WeightBase42,uom43,WeightValue44,uom45,WeightBase46,uom47,VolumeValue48,uom49,VolumeBase50,uom51,VolumeValue52,uom53,VolumeBase54,uom55,PieceCount,HandlingUnitCount,IsHazmat56
My-Organization,PythonTest1,Planning,New,PPD,FALSE,3/17/2016 13:30,3/21/2016 20:00,30000,Lb,30000,Lb,30000,Lb,30000,Lb,2100,CuFt,2100,CuFt,2100,CuFt,2100,CuFt,2100,26,FALSE,FALSE,0,USD,0,USD,FALSE,FALSE,New,New,DC_OH,My-Organization,Test,Client,DC_OH,ShipReceive,TRUE,FALSE,FALSE,FALSE,DC_OH,--,Hamilton,OH,US,45014,Mon-Fri-8-5,24/7 Appointment,FALSE,FALSE,FALSE,CZ_906,My-Organization,Test,Client,CZ_906,ShipReceive,TRUE,FALSE,FALSE,FALSE,7-ELEVEN CDC C/O GENESIS LOGISTICS,--,Santa Fe Springs,CA,US,90670,Mon-Fri-8-5,24/7 Appointment,FALSE,FALSE,FALSE,My-Organization,Test,Test,Client,TRUE,1,30000,Lb,30000,Lb,30000,Lb,30000,Lb,2100,CuFt,2100,CuFt,2100,CuFt,2100,CuFt,1170,26,FALSE
My-Organization,PythonTest2,Planning,New,PPD,FALSE,3/16/2016 14:00,3/21/2016 21:00,25000,Lb,25000,Lb,25000,Lb,25000,Lb,2300,CuFt,2300,CuFt,2300,CuFt,2300,CuFt,2300,26,FALSE,FALSE,0,USD,0,USD,FALSE,FALSE,New,New,DC_KY,My-Organization,Test,Client,DC_KY,ShipReceive,TRUE,FALSE,FALSE,FALSE,DC_KY,--,Florence,KY,US,41042,Mon-Fri-8-5,24/7 Appointment,FALSE,FALSE,FALSE,CZ_906,My-Organization,Test,Client,CZ_906,ShipReceive,TRUE,FALSE,FALSE,FALSE,7-ELEVEN CDC C/O GENESIS LOGISTICS,--,Santa Fe Springs,CA,US,90670,Mon-Fri-8-5,24/7 Appointment,FALSE,FALSE,FALSE,My-Organization,Test,Test,Client,TRUE,1,25000,Lb,25000,Lb,25000,Lb,25000,Lb,2300,CuFt,2300,CuFt,2300,CuFt,2300,CuFt,1170,26,FALSE
这是它应该如何看:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:OrderData xmlns:ns1="http://schemas.3gtms.com/tms/v1/tns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Orders>
<Order>
<OrganizationName>My-Organization</OrganizationName>
<OrdNum>PythonTest1</OrdNum>
<OrdType>Planning</OrdType>
<OrderTMSStatus>New</OrderTMSStatus>
<FreightTerms>PPD</FreightTerms>
<IsPrePayment>false</IsPrePayment>
<ScheduledEarlyPickup>2016-03-17T13:30:00.000</ScheduledEarlyPickup>
<ScheduledEarlyDelivery>2016-03-21T20:00:00.000</ScheduledEarlyDelivery>
<TotalGrossWeight>
<WeightValue uom="Lb">30000</WeightValue>
<WeightBase uom="Lb">30000</WeightBase>
</TotalGrossWeight>
<TotalNetWeight>
<WeightValue uom="Lb">30000</WeightValue>
<WeightBase uom="Lb">30000</WeightBase>
</TotalNetWeight>
<TotalGrossVolume>
<VolumeValue uom="CuFt">2100</VolumeValue>
<VolumeBase uom="CuFt">2100</VolumeBase>
</TotalGrossVolume>
<TotalNetVolume>
<VolumeValue uom="CuFt">2100</VolumeValue>
<VolumeBase uom="CuFt">2100</VolumeBase>
</TotalNetVolume>
<TotalPieceCount>2100</TotalPieceCount>
<TotalHandlingUnitCount>26</TotalHandlingUnitCount>
<IsInPlanning>false</IsInPlanning>
<AreTotalsOverridden>false</AreTotalsOverridden>
<FreightValue>
<CurrencyValue uom="USD">0</CurrencyValue>
<CurrencyBase uom="USD">0</CurrencyBase>
</FreightValue>
<IsHot>false</IsHot>
<IsHazmat>false</IsHazmat>
<BillingStatus>New</BillingStatus>
<IntegrationStatus>New</IntegrationStatus>
<OriginLocNum>DC_OH</OriginLocNum>
<OriginLoc>
<OrganizationName>My-Organization</OrganizationName>
<TradingPartnerNum>Test</TradingPartnerNum>
<TradingPartnerType>Client</TradingPartnerType>
<LocNum>DC_OH</LocNum>
<LocationType>ShipReceive</LocationType>
<IsActive>true</IsActive>
<IsBillTo>false</IsBillTo>
<IsRemitTo>false</IsRemitTo>
<IsCorporate>false</IsCorporate>
<AddrName>DC_OH</AddrName>
<Addr1>--</Addr1>
<CityName>Hamilton</CityName>
<StateCode>OH</StateCode>
<CountryISO2>US</CountryISO2>
<PostalCode>45014</PostalCode>
<CalendarName>Mon-Fri-8-5</CalendarName>
<CalendarAppointmentName>24/7 Appointment</CalendarAppointmentName>
<AllowsHazmat>false</AllowsHazmat>
<IsDeliveryAptRequired>false</IsDeliveryAptRequired>
<IsPickupAptRequired>false</IsPickupAptRequired>
</OriginLoc>
<DestinationLocNum>CZ_906</DestinationLocNum>
<DestinationLoc>
<OrganizationName>My-Organization</OrganizationName>
<TradingPartnerNum>Test</TradingPartnerNum>
<TradingPartnerType>Client</TradingPartnerType>
<LocNum>CZ_906</LocNum>
<LocationType>ShipReceive</LocationType>
<IsActive>true</IsActive>
<IsBillTo>false</IsBillTo>
<IsRemitTo>false</IsRemitTo>
<IsCorporate>false</IsCorporate>
<AddrName>7-ELEVEN CDC C/O GENESIS LOGISTICS</AddrName>
<Addr1>--</Addr1>
<CityName>Santa Fe Springs</CityName>
<StateCode>CA</StateCode>
<CountryISO2>US</CountryISO2>
<PostalCode>90670</PostalCode>
<CalendarName>Mon-Fri-8-5</CalendarName>
<CalendarAppointmentName>24/7 Appointment</CalendarAppointmentName>
<AllowsHazmat>false</AllowsHazmat>
<IsDeliveryAptRequired>false</IsDeliveryAptRequired>
<IsPickupAptRequired>false</IsPickupAptRequired>
</DestinationLoc>
<Client>
<OrganizationName>My-Organization</OrganizationName>
<TradingPartnerNum>Test</TradingPartnerNum>
<TradingPartnerName>Test</TradingPartnerName>
<TradingPartnerType>Client</TradingPartnerType>
<IsActive>true</IsActive>
</Client>
<OrderLines>
<OrderLine>
<OrdLineNum>1</OrdLineNum>
<WeightGross>
<WeightValue uom="Lb">30000</WeightValue>
<WeightBase uom="Lb">30000</WeightBase>
</WeightGross>
<WeightNet>
<WeightValue uom="Lb">30000</WeightValue>
<WeightBase uom="Lb">30000</WeightBase>
</WeightNet>
<VolumeGross>
<VolumeValue uom="CuFt">2100</VolumeValue>
<VolumeBase uom="CuFt">2100</VolumeBase>
</VolumeGross>
<VolumeNet>
<VolumeValue uom="CuFt">2100</VolumeValue>
<VolumeBase uom="CuFt">2100</VolumeBase>
</VolumeNet>
<PieceCount>1170</PieceCount>
<HandlingUnitCount>26</HandlingUnitCount>
<IsHazmat>false</IsHazmat>
</OrderLine>
</OrderLines>
</Order>
<Order>
<OrganizationName>My-Organization</OrganizationName>
<OrdNum>PythonTest2</OrdNum>
<OrdType>Planning</OrdType>
<OrderTMSStatus>New</OrderTMSStatus>
<FreightTerms>PPD</FreightTerms>
<IsPrePayment>false</IsPrePayment>
<ScheduledEarlyPickup>2016-03-16T14:00:00.000</ScheduledEarlyPickup>
<ScheduledEarlyDelivery>2016-03-21T21:00:00.000</ScheduledEarlyDelivery>
<TotalGrossWeight>
<WeightValue uom="Lb">25000</WeightValue>
<WeightBase uom="Lb">25000</WeightBase>
</TotalGrossWeight>
<TotalNetWeight>
<WeightValue uom="Lb">25000</WeightValue>
<WeightBase uom="Lb">25000</WeightBase>
</TotalNetWeight>
<TotalGrossVolume>
<VolumeValue uom="CuFt">2300</VolumeValue>
<VolumeBase uom="CuFt">2300</VolumeBase>
</TotalGrossVolume>
<TotalNetVolume>
<VolumeValue uom="CuFt">2300</VolumeValue>
<VolumeBase uom="CuFt">2300</VolumeBase>
</TotalNetVolume>
<TotalPieceCount>2300</TotalPieceCount>
<TotalHandlingUnitCount>26</TotalHandlingUnitCount>
<IsInPlanning>false</IsInPlanning>
<AreTotalsOverridden>false</AreTotalsOverridden>
<FreightValue>
<CurrencyValue uom="USD">0</CurrencyValue>
<CurrencyBase uom="USD">0</CurrencyBase>
</FreightValue>
<IsHot>false</IsHot>
<IsHazmat>false</IsHazmat>
<BillingStatus>New</BillingStatus>
<IntegrationStatus>New</IntegrationStatus>
<OriginLocNum>DC_KY</OriginLocNum>
<OriginLoc>
<OrganizationName>My-Organization</OrganizationName>
<TradingPartnerNum>Test</TradingPartnerNum>
<TradingPartnerType>Client</TradingPartnerType>
<LocNum>DC_KY</LocNum>
<LocationType>ShipReceive</LocationType>
<IsActive>true</IsActive>
<IsBillTo>false</IsBillTo>
<IsRemitTo>false</IsRemitTo>
<IsCorporate>false</IsCorporate>
<AddrName>DC_KY</AddrName>
<Addr1>--</Addr1>
<CityName>Florence</CityName>
<StateCode>KY</StateCode>
<CountryISO2>US</CountryISO2>
<PostalCode>41042</PostalCode>
<CalendarName>Mon-Fri-8-5</CalendarName>
<CalendarAppointmentName>24/7 Appointment</CalendarAppointmentName>
<AllowsHazmat>false</AllowsHazmat>
<IsDeliveryAptRequired>false</IsDeliveryAptRequired>
<IsPickupAptRequired>false</IsPickupAptRequired>
</OriginLoc>
<DestinationLocNum>CZ_906</DestinationLocNum>
<DestinationLoc>
<OrganizationName>My-Organization</OrganizationName>
<TradingPartnerNum>Test</TradingPartnerNum>
<TradingPartnerType>Client</TradingPartnerType>
<LocNum>CZ_906</LocNum>
<LocationType>ShipReceive</LocationType>
<IsActive>true</IsActive>
<IsBillTo>false</IsBillTo>
<IsRemitTo>false</IsRemitTo>
<IsCorporate>false</IsCorporate>
<AddrName>7-ELEVEN CDC C/O GENESIS LOGISTICS</AddrName>
<Addr1>--</Addr1>
<CityName>Santa Fe Springs</CityName>
<StateCode>CA</StateCode>
<CountryISO2>US</CountryISO2>
<PostalCode>90670</PostalCode>
<CalendarName>Mon-Fri-8-5</CalendarName>
<CalendarAppointmentName>24/7 Appointment</CalendarAppointmentName>
<AllowsHazmat>false</AllowsHazmat>
<IsDeliveryAptRequired>false</IsDeliveryAptRequired>
<IsPickupAptRequired>false</IsPickupAptRequired>
</DestinationLoc>
<Client>
<OrganizationName>My-Organization</OrganizationName>
<TradingPartnerNum>Test</TradingPartnerNum>
<TradingPartnerName>Test</TradingPartnerName>
<TradingPartnerType>Client</TradingPartnerType>
<IsActive>true</IsActive>
</Client>
<OrderLines>
<OrderLine>
<OrdLineNum>1</OrdLineNum>
<WeightGross>
<WeightValue uom="Lb">25000</WeightValue>
<WeightBase uom="Lb">25000</WeightBase>
</WeightGross>
<WeightNet>
<WeightValue uom="Lb">25000</WeightValue>
<WeightBase uom="Lb">25000</WeightBase>
</WeightNet>
<VolumeGross>
<VolumeValue uom="CuFt">2300</VolumeValue>
<VolumeBase uom="CuFt">2300</VolumeBase>
</VolumeGross>
<VolumeNet>
<VolumeValue uom="CuFt">2300</VolumeValue>
<VolumeBase uom="CuFt">2300</VolumeBase>
</VolumeNet>
<PieceCount>1170</PieceCount>
<HandlingUnitCount>26</HandlingUnitCount>
<IsHazmat>false</IsHazmat>
</OrderLine>
</OrderLines>
</Order>
</Orders>
帮助深表感谢。
如果它是任何人都试图做同样的事情有帮助的,这是你如何上传有效的XML的系统:
import requests
filename = 'somefile.xml'
api_url = 'someurl.com'
headers = {'Content-Type': 'application/xml'}
response = requests.post(api_url, data=open(filename).read(), headers=headers)
这可能有助于。将.csv读取到熊猫数据框,然后使用这种方法。 http://stackoverflow.com/questions/18574108/how-do-convert-a-pandas-dataframe-to-xml] – scomes
我实际上使用,作为我的第一个解决方案,但它不工作,因为我创建我的自己的架构在这个例子..在我的例子中,我有一个预先存在的架构,我需要映射到我的文件。 – fmolino