![]() ![]() Writer = csv.DictWriter(f, fields, delimiter=" ", extrasaction="ignore") Iterate over elements "TrdCapRpt", write attribute values to CSV file: with open(csv_fname, "w") as f: Read the XML: xml = etree.parse(xml_fname) ![]() "SettlDt", "PxSubTyp", "VenueTyp", "VenuTyp", "OfstInst"] "MtchStat" "MsgEvtSrc", "TrdID", "LastQty", "LastPx", "TxnTm", "SettlCcy", "RptID", "TrdTyp", "TrdSubTyp", "ExecID", "TrdDt", "BizDt", "MLegRptTyp", Holds a dictionary with key/value for each attribute.Ĭsv.DictWriter allows writing data taken from dictionary.įirst some imports (I always use lxml as it is very fast and provides extra features): from lxml import etreeĬonfigure file names and fields to use in each record: xml_fname = "data.xml" Your elements named TrdCapRpt have attributes, if you have such node, its attribute node.attrib Use csv.DictWriter, get values from node.attrib dictionary Is there a easy way to export this to a CSV ? Spamwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) With open('/Users/anantsangar/Desktop/output.csv', 'w') as csvfile: ![]() Print RptID, TrdTyp, TrdSubTyp, TrdDt, BizDt, MLegRptTyp, MtchStat, MsgEvtSrc, TrdID, LastQty, LastPx, TxnTm, SettlCcy, SettlDt, PxSubTyp, VenueTyp, VenuTyp, OfstInst With open('/Users/testuser/Desktop/CMEREG1.XML', 'rt') as f: I have tried it with the following code but I can t get the right output: from xml.etree import ElementTree I have the following XML structure and I am trying to convert it into a csv in python: Īnd I am trying to convert this into a csv file. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |