hari krishna
asked on
First I have to parse XML file and then i have covert the XML file to CSV with ~ separtor
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StandingSettlementInstruc tion xmlns="http://www.service.cdds.cs.com/ssiData_2">
<StableChangeVersion>2</St ableChange Version>
<Core>
<LocalId>0171375670</Local Id>
<EffectiveDate>2018-06-24T 06:38:47.0 93-04:00</ EffectiveD ate>
<DataStatus>Active</DataSt atus>
<InternalIdentifier>000000 00-0000-00 00-0000-00 0008180d98 </Internal Identifier >
</Core>
<Change>
<LastApprovedVersion>2</La stApproved Version>
<LatestReviewTime>2018-06- 24T06:38:4 7.013-04:0 0</LatestR eviewTime>
</Change>
<SystemCodes>
<SystemCodeType>
<Id>NTPA</Id>
</SystemCodeType>
<Value>Y37F40</Value>
<ValueStatus>Active</Value Status>
</SystemCodes>
<LatestReviewers>
<userName>msiepra</userNam e>
</LatestReviewers>
<TradingAccount>
<Core>
<LocalId>0170485038</Local Id>
</Core>
<SystemCodes>
<SystemCodeType>
<Id>TradingAccountGSID</Id >
</SystemCodeType>
<Value>4973662</Value>
<ValueStatus>Active</Value Status>
</SystemCodes>
<SystemCodes>
<SystemCodeType>
<Id>NTPA</Id>
</SystemCodeType>
<Value>Y37F40</Value>
<ValueStatus>Active</Value Status>
</SystemCodes>
<SystemCodes>
<SystemCodeType>
<Id>FXOPs</Id>
</SystemCodeType>
<Value>L61774</Value>
<ValueStatus>Active</Value Status>
</SystemCodes>
<SystemCodes>
<SystemCodeType>
<Id>SDSFB</Id>
</SystemCodeType>
<Value>MARSH38LONGB</Value >
<ValueStatus>Active</Value Status>
</SystemCodes>
</TradingAccount>
<Country>
<IsoCode>DE</IsoCode>
</Country>
<Security>
<Id>COB</Id>
</Security>
<SettlementMethod>
<Id>EURO_CLEAR</Id>
</SettlementMethod>
<Currency>
<IsoCode>EUR</IsoCode>
</Currency>
<DefaultSsi>true</DefaultS si>
<TermsOfDelivery>
<Id>FOP</Id>
</TermsOfDelivery>
<SettlementType>NON_CASH</ Settlement Type>
<ClassOfAccount>BOTH</Clas sOfAccount >
<SettlementChainEntries>
<StepType>NonCashDeagReag< /StepType>
<IdentifierType>
<Id>SWIFT_BIC</Id>
</IdentifierType>
</SettlementChainEntries>
<SettlementChainEntries>
<StepType>NonCashDecuRecu< /StepType>
<IdentifierType>
<Id>SWIFT_BIC</Id>
</IdentifierType>
</SettlementChainEntries>
<SettlementChainEntries>
<StepType>NonCashPset</Ste pType>
<Bic>MGTCBEBEECL</Bic>
<AccountNumber>47950</Acco untNumber>
<IdentifierType>
<Id>SWIFT_BIC</Id>
</IdentifierType>
</SettlementChainEntries>
<RecDel>BOTH</RecDel>
<CptyFirm>CPTY</CptyFirm>
<CountryOfSettlement/>
<SettleTypeOverride>
<Id>EE</Id>
</SettleTypeOverride>
</StandingSettlementInstru ction>
First I have to parse XML file and then
I have an XML file I need convert this into csv file with '~' seperator
Could you please help with the Code.
example output CSV.
StableChangeVersion ~ Core~Local id~trandingaccount~systemc ode
2 ~ NA~124~8764~hari
Many Thanks in Advance.
Hari
<StandingSettlementInstruc
<StableChangeVersion>2</St
<Core>
<LocalId>0171375670</Local
<EffectiveDate>2018-06-24T
<DataStatus>Active</DataSt
<InternalIdentifier>000000
</Core>
<Change>
<LastApprovedVersion>2</La
<LatestReviewTime>2018-06-
</Change>
<SystemCodes>
<SystemCodeType>
<Id>NTPA</Id>
</SystemCodeType>
<Value>Y37F40</Value>
<ValueStatus>Active</Value
</SystemCodes>
<LatestReviewers>
<userName>msiepra</userNam
</LatestReviewers>
<TradingAccount>
<Core>
<LocalId>0170485038</Local
</Core>
<SystemCodes>
<SystemCodeType>
<Id>TradingAccountGSID</Id
</SystemCodeType>
<Value>4973662</Value>
<ValueStatus>Active</Value
</SystemCodes>
<SystemCodes>
<SystemCodeType>
<Id>NTPA</Id>
</SystemCodeType>
<Value>Y37F40</Value>
<ValueStatus>Active</Value
</SystemCodes>
<SystemCodes>
<SystemCodeType>
<Id>FXOPs</Id>
</SystemCodeType>
<Value>L61774</Value>
<ValueStatus>Active</Value
</SystemCodes>
<SystemCodes>
<SystemCodeType>
<Id>SDSFB</Id>
</SystemCodeType>
<Value>MARSH38LONGB</Value
<ValueStatus>Active</Value
</SystemCodes>
</TradingAccount>
<Country>
<IsoCode>DE</IsoCode>
</Country>
<Security>
<Id>COB</Id>
</Security>
<SettlementMethod>
<Id>EURO_CLEAR</Id>
</SettlementMethod>
<Currency>
<IsoCode>EUR</IsoCode>
</Currency>
<DefaultSsi>true</DefaultS
<TermsOfDelivery>
<Id>FOP</Id>
</TermsOfDelivery>
<SettlementType>NON_CASH</
<ClassOfAccount>BOTH</Clas
<SettlementChainEntries>
<StepType>NonCashDeagReag<
<IdentifierType>
<Id>SWIFT_BIC</Id>
</IdentifierType>
</SettlementChainEntries>
<SettlementChainEntries>
<StepType>NonCashDecuRecu<
<IdentifierType>
<Id>SWIFT_BIC</Id>
</IdentifierType>
</SettlementChainEntries>
<SettlementChainEntries>
<StepType>NonCashPset</Ste
<Bic>MGTCBEBEECL</Bic>
<AccountNumber>47950</Acco
<IdentifierType>
<Id>SWIFT_BIC</Id>
</IdentifierType>
</SettlementChainEntries>
<RecDel>BOTH</RecDel>
<CptyFirm>CPTY</CptyFirm>
<CountryOfSettlement/>
<SettleTypeOverride>
<Id>EE</Id>
</SettleTypeOverride>
</StandingSettlementInstru
First I have to parse XML file and then
I have an XML file I need convert this into csv file with '~' seperator
Could you please help with the Code.
example output CSV.
StableChangeVersion ~ Core~Local id~trandingaccount~systemc
2 ~ NA~124~8764~hari
Many Thanks in Advance.
Hari
ASKER
I have tried using the below scripts;
import os, sys
import csv
import xml.etree.ElementTree as ET
file_name='SSI_sample.txt'
cd = os.path.dirname(os.path.ab spath(file _name))
#data_from_file = open(etree, 'r')
NAMESPACE = 'http://www.service.cdds.cs.com/ssiData_2'
#def remove_name_space(value):
# cleaned_key = re.sub(NAMESPACE, '', value)
# return cleaned_key
#def add_name_space(value):
# add_key = (NAMESPACE + value)
# return add_key
#def add_name_space_to_xpath(va lue):
# path_list = value.split('/')
# result_xpath = []
# for path_keyword in path_list:
# result_xpath.append(add_na me_space(p ath_keywor d))
#return '/'.join(result_xpath)
#def find_attribute_value_from_ xpath(xpat h_value):
# result = dom.findall(add_name_space _to_xpath( xpath_valu e))
# for result_iter in result:
# return result_iter.text
# ITERATE THROUGH ALL XML FILES
for item in os.listdir(cd):
if item.endswith(".xml"):
tree = ET.parse(os.path.join(cd,i tem))
subroot = tree.xpath("//StableChange Version")
with open(os.path.join(cd,'Mult ipleXPaths .csv'), 'ab') as m:
writer = csv.writer(m)
for i in range(1,len(subroot)+1):
nodes = tree.xpath('//StableChange Version[{0 }]//*'.for mat(i))
cols = []
rows = []
for elem in nodes:
cols.append(elem.tag)
rows.append(elem.text.repl ace('\n',' ').strip() )
if i == 1:
print (', '.join(cols)+"\n")
writer.writerow(cols)
print (', '.join(rows)+"\n")
writer.writerow(rows)
But it is not working.
The script is given by you is working fine but the thing . How can I use Xpath for the same.
import os, sys
import csv
import xml.etree.ElementTree as ET
file_name='SSI_sample.txt'
cd = os.path.dirname(os.path.ab
#data_from_file = open(etree, 'r')
NAMESPACE = 'http://www.service.cdds.cs.com/ssiData_2'
#def remove_name_space(value):
# cleaned_key = re.sub(NAMESPACE, '', value)
# return cleaned_key
#def add_name_space(value):
# add_key = (NAMESPACE + value)
# return add_key
#def add_name_space_to_xpath(va
# path_list = value.split('/')
# result_xpath = []
# for path_keyword in path_list:
# result_xpath.append(add_na
#return '/'.join(result_xpath)
#def find_attribute_value_from_
# result = dom.findall(add_name_space
# for result_iter in result:
# return result_iter.text
# ITERATE THROUGH ALL XML FILES
for item in os.listdir(cd):
if item.endswith(".xml"):
tree = ET.parse(os.path.join(cd,i
subroot = tree.xpath("//StableChange
with open(os.path.join(cd,'Mult
writer = csv.writer(m)
for i in range(1,len(subroot)+1):
nodes = tree.xpath('//StableChange
cols = []
rows = []
for elem in nodes:
cols.append(elem.tag)
rows.append(elem.text.repl
if i == 1:
print (', '.join(cols)+"\n")
writer.writerow(cols)
print (', '.join(rows)+"\n")
writer.writerow(rows)
But it is not working.
The script is given by you is working fine but the thing . How can I use Xpath for the same.
ASKER
Hi flabio,
I have facing the issue in the code you have sent .
There are Local id in the XML file . But I am able the get for one local id for the Core tag.
there is another local Id in trading account tag.
<Core>
<LocalId>0171375670</Local Id>
<EffectiveDate>2018-06-24T 06:38:47.0 93-04:00</ EffectiveD ate>
<DataStatus>Active</DataSt atus>
<TradingAccount>
<Core>
<LocalId>0170485038</Local Id>
</Core>
How we can get both Local id in the output
I have facing the issue in the code you have sent .
There are Local id in the XML file . But I am able the get for one local id for the Core tag.
there is another local Id in trading account tag.
<Core>
<LocalId>0171375670</Local
<EffectiveDate>2018-06-24T
<DataStatus>Active</DataSt
<TradingAccount>
<Core>
<LocalId>0170485038</Local
</Core>
How we can get both Local id in the output
How we can get both Local id in the outputYou start from TradingAccount to get the 2nd LocalId.
TradingAccountLocalId = tree.find('{http://www.service.cdds.cs.com/ssiData_2}TradingAccount/{http://www.service.cdds.cs.com/ssiData_2}Core/{http://www.service.cdds.cs.com/ssiData_2}LocalId')
ASKER
Many thanks Gates.
As per your code i able to see data . But i want to generate the CSV file in the server with Same ouput.
Could you please help in this
As per your code i able to see data . But i want to generate the CSV file in the server with Same ouput.
Could you please help in this
ASKER
Hi Gates,
I have written the code to covert XML file with CSV ~ separator. But I am unable to get CSV output file .
My code:
import xml.etree.ElementTree as ET
import csv
tree = ET.parse("SSI_sample.txt")
root = tree.getroot()
xml_data_to_csv=open('SSId ata.csv',' w')
list_head = []
count=0
for element in root .findall('{http://www.service.cdds.cs.com/ssiData_2}StandingSettle mentInstru ction'):
list_nodes=[]
if count==0:
StableChangeVersion = element.find('{http://www.service.cdds.cs.com/ssiData_2}StableChan geVersion' ).tag
list_head.append(StableCha ngeVersion )
LocalId = element.find('{http://www.service.cdds.cs.com/ssiData_2}Core/{http://www.service.cdds.cs.com/ssiData_2}Loc alId').tag
list_head.append(LocalId)
TradingAccountLocalId = element.find('{http://www.service.cdds.cs.com/ssiData_2}TradingAcc ount/{http://www.service.cdds.cs.com/ssiData_2}Cor e/{http://www.service.cdds.cs.com/ssiData_2}LocalI d').tag
list_head.append(TradingAc countLocal Id)
csv_writer.writer(list_hea d)
count=+1
StableChangeVersion=elemen t.find('{http://www.service.cdds.cs.com/ssiData_2} StableChan geVersion' ).text
list_nodes.append(StableCh angeVersio n)
LocalId = element.find('{http://www.service.cdds.cs.com/ssiData_2}Core/{http://www.service.cdds.cs.com/ssiData_2}Loc alId').tex t
list_nodes.append(LocalId)
TradingAccountLocalId = element.find('{http://www.service.cdds.cs.com/ssiData_2}TradingAcc ount/{http://www.service.cdds.cs.com/ssiData_2}Cor e/{http://www.service.cdds.cs.com/ssiData_2}LocalI d').text
list_nodes.append(TradingA ccountLoca lId)
csv_writer.writer(list_nod es)
xml_data_to_csv.close()
##etree.dump(item)
could you pls help in this.
Many Thanks,
Hari.
I have written the code to covert XML file with CSV ~ separator. But I am unable to get CSV output file .
My code:
import xml.etree.ElementTree as ET
import csv
tree = ET.parse("SSI_sample.txt")
root = tree.getroot()
xml_data_to_csv=open('SSId
list_head = []
count=0
for element in root .findall('{http://www.service.cdds.cs.com/ssiData_2}StandingSettle
list_nodes=[]
if count==0:
StableChangeVersion = element.find('{http://www.service.cdds.cs.com/ssiData_2}StableChan
list_head.append(StableCha
LocalId = element.find('{http://www.service.cdds.cs.com/ssiData_2}Core/{http://www.service.cdds.cs.com/ssiData_2}Loc
list_head.append(LocalId)
TradingAccountLocalId = element.find('{http://www.service.cdds.cs.com/ssiData_2}TradingAcc
list_head.append(TradingAc
csv_writer.writer(list_hea
count=+1
StableChangeVersion=elemen
list_nodes.append(StableCh
LocalId = element.find('{http://www.service.cdds.cs.com/ssiData_2}Core/{http://www.service.cdds.cs.com/ssiData_2}Loc
list_nodes.append(LocalId)
TradingAccountLocalId = element.find('{http://www.service.cdds.cs.com/ssiData_2}TradingAcc
list_nodes.append(TradingA
csv_writer.writer(list_nod
xml_data_to_csv.close()
##etree.dump(item)
could you pls help in this.
Many Thanks,
Hari.
ASKER
Hi Gates,
My Apologies for asking so more help in writing the Code . First of all many thanks for the code you have given. could you please help . I am facing one more issue.
SystemCodes>
<SystemCodeType>
<Id>TradingAccountGSID</Id >
</SystemCodeType>
<Value>4973662</Value>
<ValueStatus>Active</Value Status>
</SystemCodes>
<SystemCodes>
<SystemCodeType>
<Id>NTPA</Id>
</SystemCodeType>
<Value>Y37F40</Value>
<ValueStatus>Active</Value Status>
</SystemCodes>
<SystemCodes>
<SystemCodeType>
<Id>FXOPs</Id>
</SystemCodeType>
<Value>L61774</Value>
<ValueStatus>Active</Value Status>
</SystemCodes>
<SystemCodes>
<SystemCodeType>
<Id>SDSFB</Id>
</SystemCodeType>
<Value>MARSH38LONGB</Value >
<ValueStatus>Active</Value Status>
</SystemCodes>
How can I get the output of all systemcodes value and valuelist and systemcodetype id
Ex SystemCodesTypeId|Value|Va lueStatus
NTPA|Y37F40|Active
SDSFB|MARSH38LONGB|Active.
Could you pls ASAP.
Many Thanks,
Hari.
My Apologies for asking so more help in writing the Code . First of all many thanks for the code you have given. could you please help . I am facing one more issue.
SystemCodes>
<SystemCodeType>
<Id>TradingAccountGSID</Id
</SystemCodeType>
<Value>4973662</Value>
<ValueStatus>Active</Value
</SystemCodes>
<SystemCodes>
<SystemCodeType>
<Id>NTPA</Id>
</SystemCodeType>
<Value>Y37F40</Value>
<ValueStatus>Active</Value
</SystemCodes>
<SystemCodes>
<SystemCodeType>
<Id>FXOPs</Id>
</SystemCodeType>
<Value>L61774</Value>
<ValueStatus>Active</Value
</SystemCodes>
<SystemCodes>
<SystemCodeType>
<Id>SDSFB</Id>
</SystemCodeType>
<Value>MARSH38LONGB</Value
<ValueStatus>Active</Value
</SystemCodes>
How can I get the output of all systemcodes value and valuelist and systemcodetype id
Ex SystemCodesTypeId|Value|Va
NTPA|Y37F40|Active
SDSFB|MARSH38LONGB|Active.
Could you pls ASAP.
Many Thanks,
Hari.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Open in new window