0

I am reading the sales order .csv file, Here is my sample csv look like

SNo,Customer_id,Itemid,Product,Quanity,Price']
1 , ABC123 , 10 , X1 , 1001 , 10001
1 , ABC123 , 20 , X2 , 1002 , 10002
2 , CBC321 , 10 , X5 , 1005 , 10005
2 , CBC321 , 20 , X6 , 1006 , 10006

In the above sample, SNo, Customer is header and Itemid, Product, Quantity, Price is subitems. I need to generate the payload in below format

<SNo>1 </SNo<Customer_idABC123 </Customer_id>
    <Itemid10 </Itemid>
    <ProductX1 </Product>
    <Quanity1001 </Quanity>
    <Price']10001</Price']>
    <Itemid20 </Itemid>
    <ProductX2 </Product>
    <Quanity1002 </Quanity>
    <Price']10002</Price']>

<SNo>2 </SNo<Customer_idCBc321</Customer_id>
    <Itemid10 </Itemid>
    <ProductX5 </Product>
    <Quanity1005 </Quanity>
    <Price']10005</Price']>
    <Itemid20 </Itemid>
    <ProductX6 </Product>
    <Quanity1006 </Quanity>
    <Price']10006</Price']>

How to generate the payload in the above format?

James Z
  • 12,209
  • 10
  • 24
  • 44
Karthi
  • 39
  • 4

2 Answers2

0

You could simply use a Jinja template string and insert the values accordingly. Not the most beautiful solution for sure...but done within a view minutes.

E.Murphy
  • 25
  • 1
  • 3
0

Please use below code

with open("input.csv","r") as f:
    head=[i.strip() for i in f.readline().split(",")]
    line=f.readline()
    while line:
        print("".join(["<%s>%s</%s>"%(i,a.strip(),i) for (i,a) in zip(head,line.split(","))]))
        line=f.readline()
Liju
  • 2,273
  • 3
  • 6
  • 21