使用Python将CSV转换为XML,以通过API提供

时间:2016-09-28 12:28:22

标签: python xml csv

我已经看到了如何将csv(或SQL表)转换为XML的几个答案,但我还没有看到一个包含层次结构且没有过于复杂的问题。我需要将csv文件映射到预先存在的XML格式,以使用Python将其提供给API。我已经可以将有效的XML发送到网站,但我首先要将csv转换为XML。

我的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)

2 个答案:

答案 0 :(得分:0)

没有通用库可以直接将CSV转换为所需的XML。您需要使用python提供的xml函数或使用xml.etree API

之类的函数来构建XML

Here's one sample link

这是一个可以实现它的流程 - &gt;

  1. 阅读CSV
  2. 创建名称空间

  3. 创建父节点<Orders>

  4. 阅读CSV。对于每一行,创建一个新节点<Order>并向其添加元素。将此节点作为子节点添加到父<Orders>

  5. 重复步骤4平铺文件结尾

答案 1 :(得分:0)

我实际上已经为此找到了一个SQL解决方案......但是对于我的情况来说,它是非常可笑的手动和特定的。我希望这种格式仍能帮助某人解决类似的问题。

如果您从下面的代码中注意到,如果您不包含别名,则XML标记将采用该列的名称。您可以使用“父母/子女”格式。对于层次结构...查看查询的底部以查看多个嵌入式层次结构。

SELECT 
    [OrganizationName]
  ,[OrdNum]
  ,[OrdType]
  ,[OrderTMSStatus]
  ,[FreightTerms]
  ,[IsPrePayment]
  ,[ScheduledEarlyPickup]
  ,[ScheduledEarlyDelivery]
  ,[WeightValue]  'TotalGrossWeight/WeightValue'
  ,[WeightBase] 'TotalGrossWeight/WeightBase'
  ,[WeightValue3]  'TotalNetWeight/WeightValue'
  ,[WeightBase5]  'TotalNetWeight/WeightBase'
  ,[VolumeValue]  'TotalGrossVolume/VolumeValue'
  ,[VolumeBase] 'TotalGrossVolume/VolumeBase'
  ,[VolumeValue9] 'TotalNetVolume/VolumeValue'
  ,[VolumeBase11] 'TotalNetVolume/VolumeBase'
  ,[TotalPieceCount]
  ,[TotalHandlingUnitCount]
  ,[IsInPlanning]
  ,[AreTotalsOverridden]
  ,[CurrencyValue] 'FreightValue/CurrencyValue'
  ,[CurrencyBase] 'FreightValue/CurrencyBase'
  ,[IsHot]
  ,[IsHazmat]
  ,[BillingStatus]
  ,[IntegrationStatus]
  ,[OriginLocNum]
  ,[OrganizationName15] 'OriginLoc/OrganizationName'
  ,[TradingPartnerNum] 'OriginLoc/TradingPartnerNum'
  ,[TradingPartnerType] 'OriginLoc/TradingPartnerType'
  ,[LocNum] 'OriginLoc/LocNum'
  ,[LocationType] 'OriginLoc/LocationType'
  ,[IsActive] 'OriginLoc/IsActive'
  ,[IsBillTo] 'OriginLoc/IsBillTo'
  ,[IsRemitTo] 'OriginLoc/IsRemitTo'
  ,[IsCorporate] 'OriginLoc/IsCorporate'
  ,[AddrName] 'OriginLoc/AddrName'
  ,[Addr1] 'OriginLoc/Addr1'
  ,[CityName] 'OriginLoc/CityName'
  ,[StateCode] 'OriginLoc/StateCode'
  ,[CountryISO2] 'OriginLoc/CountryISO2'
  ,[PostalCode] 'OriginLoc/PostalCode'
  ,[CalendarName] 'OriginLoc/CalendarName'
  ,[CalendarAppointmentName] 'OriginLoc/CalendarAppointmentName'
  ,[AllowsHazmat] 'OriginLoc/AllowsHazmat'
  ,[IsDeliveryAptRequired] 'OriginLoc/IsDeliveryAptRequired'
  ,[IsPickupAptRequired] 'OriginLoc/IsPickupAptRequired'
  ,[DestinationLocNum] 
  ,[OrganizationName16] 'DestinationLoc/OrganizationName'
  ,[TradingPartnerNum17] 'DestinationLoc/TradingPartnerNum'
  ,[TradingPartnerType18] 'DestinationLoc/TradingPartnerType'
  ,[LocNum19] 'DestinationLoc/LocNum'
  ,[LocationType20] 'DestinationLoc/LocationType'
  ,[IsActive21] 'DestinationLoc/IsActive'
  ,[IsBillTo22] 'DestinationLoc/IsBillTo'
  ,[IsRemitTo23] 'DestinationLoc/IsRemitTo'
  ,[IsCorporate24] 'DestinationLoc/IsCorporate'
  ,[AddrName25] 'DestinationLoc/AddrName'
  ,[Addr126] 'DestinationLoc/Addr1'
  ,[CityName27] 'DestinationLoc/CityName'
  ,[StateCode28] 'DestinationLoc/StateCode'
  ,[CountryISO229] 'DestinationLoc/CountryISO2'
  ,[PostalCode30] 'DestinationLoc/PostalCode'
  ,[CalendarName31] 'DestinationLoc/CalendarName'
  ,[CalendarAppointmentName32] 'DestinationLoc/CalendarAppointmentName'
  ,[AllowsHazmat33] 'DestinationLoc/AllowsHazmat'
  ,[IsDeliveryAptRequired34] 'DestinationLoc/IsDeliveryAptRequired'
  ,[IsPickupAptRequired35] 'DestinationLoc/IsPickupAptRequired' 
  ,[OrganizationName36] 'Client/OrganizationName'
  ,[TradingPartnerNum37] 'Client/TradingPartnerNum'
  ,[TradingPartnerName] 'Client/TradingPartnerName'
  ,[TradingPartnerType38] 'Client/TradingPartnerType'
  ,[IsActive39] 'Client/IsActive'
  ,[OrdLineNum] 'OrderLines/OrderLine/OrdLineNum'
  ,[WeightValue40] 'OrderLines/OrderLine/WeightGross/WeightValue'
  ,[WeightBase42] 'OrderLines/OrderLine/WeightGross/WeightBase'
  ,[WeightValue44] 'OrderLines/OrderLine/WeightNet/WeightValue'
  ,[WeightBase46] 'OrderLines/OrderLine/WeightNet/WeightBase'
  ,[VolumeValue48] 'OrderLines/OrderLine/VolumeGross/VolumeValue'
  ,[VolumeBase50] 'OrderLines/OrderLine/VolumeGross/VolumeBase'
  ,[VolumeValue52] 'OrderLines/OrderLine/VolumeNet/VolumeValue'
  ,[VolumeBase54] 'OrderLines/OrderLine/VolumeNet/VolumeBase'
  ,[PieceCount] 'OrderLines/OrderLine/PieceCount'
  ,[HandlingUnitCount] 'OrderLines/OrderLine/HandlingUnitCount'
  ,[IsHazmat56] 'OrderLines/OrderLine/IsHazmat'
 FROM [MyDatabase].[dbo].[OrderSample]
 For XML PATH ('Order')

我使用pymssql库在Python内部运行该脚本

#Create a function that reads SQL Query file and returns the result as a string
def queryReturn(query=sql, connection = conn):
    '''
    This function runs a SQL File and returns the result as a strings
    It takes in a T-SQL file as the first argument
    For example:  
    sql=open('sqlFinal.sql', 'r').read().decode('utf-8')
    xml = queryReturn(sql)
    You can also define which database connection to use
    For example: xml = queryReturn(sql,conn)

    '''
    cursor = connection.cursor()
    cursor.execute(query)
    result= "".join([item[0] for item in cursor.fetchall()])
    return result

#Create the header and tail for the XML
header ='''<?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>\n '''
tail = '''\n    </Orders>
</ns1:OrderData> '''

#Add uom to the opening tags that need it
xml2=xml2.replace('<WeightValue','<WeightValue uom="Lb"')
xml2=xml2.replace('<WeightBase','<WeightBase uom="Lb"')
xml2=xml2.replace('<VolumeValue','<VolumeValue uom="CuFt"')
xml2=xml2.replace('<VolumeBase','<VolumeBase uom="CuFt"')
xml2=xml2.replace('<CurrencyValue','<CurrencyValue uom="USD"')
xml2=xml2.replace('<CurrencyBase','<CurrencyBase uom="USD"')

然后我将查询的顶部和底部添加到主体:

#create final XML string
final = header+xml2+tail