Python CSV read-> write; remove and replace PLUS: end of line is JSON format
Question: I am having problems getting my Python script to do what I want. It does not
appear to be modifying my file.

I want to:

  1. Read in a *.csv file that has the following format PropertyName::PropertyValue,…,PropertyName::PropertyValue,{ExtPropertyName::ExtPropertyValue},…,{ExtPropertyName:: ExtPropertyValue}
  2. I want to remove PropertyName:: and leave behid just a column of the PropertyValue
  3. I want to add a header line

I was trying to step through replacing the :: values with a comma, but cant
seem to get this to work:

    
    
    fin = csv.reader(open('infile', 'rb'), delimiter=',')
    fout = open('outfile', 'w')
    for row in fin:
       fout.write(','.join(','.join(item.split()) for item in row) + '::')
    fout.close()
    

Any advice, whether on my first step problem, or to a bigger picture
resolution is always appreciated. Thanks.

UPDATE/EDIT asked for by a person nice enough to review for me!

Here is the first line of the *.csv file (INPUT)

    
    
    InnerDiameterOrWidth::0.1,InnerHeight::0.1,Length2dCenterToCenter::44.6743867864386,Length3dCenterToCenter::44.6768028159989,Length2dToInsideEdge::44.2678260053526,Length3dToInsideEdge::44.2717800813466,Length2dToOutsideEdge::44.6743867864386,Length3dToOutsideEdge::44.6768028159989,MinimumCover::0,MaximumCover::0,StartConnection::ImmxGisUtilityNetworkCommon.Connection,
    

In a perfect world here is what I would like my text file to look like
(OUTPUT)

    
    
    InnerDiameterOrWidth, InnerHeight, Length2dCenterToCenter,,,,,,,,,,,
    0.1,0.1,44.6743867864386
    

so one header line and the values in column

**UPDATED** JSON Info

The end of each line has JSON formatted text:

    
    
    {StartPoint::7858.35924983374[%2C]1703.69341358077[%2C]-3.075},{EndPoint::7822.85045874375[%2C]1730.80294308742[%2C]-3.53962362760298}
    

WHich I need to split into X Y Z and X Y Z with headers


Answer: Maybe something like this (assuming that each line has the same keys, and in
the same order):

    
    
    import csv
    
    with open("diam.csv", "rb") as fin, open("diam_out.csv", "wb") as fout:
        reader = csv.reader(fin)
        writer = csv.writer(fout)
        for i, line in enumerate(reader):
            split = [item.split("::") for item in line if item.strip()]
            if not split: # blank line
                continue
            keys, vals = zip(*split)
            if i == 0:
                # first line: write header
                writer.writerow(keys)
            writer.writerow(vals)
    

which produces

    
    
    localhost-2:coding $ cat diam_out.csv 
    InnerDiameterOrWidth,InnerHeight,Length2dCenterToCenter,Length3dCenterToCenter,Length2dToInsideEdge,Length3dToInsideEdge,Length2dToOutsideEdge,Length3dToOutsideEdge,MinimumCover,MaximumCover,StartConnection
    0.1,0.1,44.6743867864386,44.6768028159989,44.2678260053526,44.2717800813466,44.6743867864386,44.6768028159989,0,0,ImmxGisUtilityNetworkCommon.Connection
    

I think most of that code should make sense, except maybe the `zip(*split)`
trick: that basically transposes a sequence, i.e.

    
    
    >>> s = [['a','1'],['b','2']]
    >>> zip(*s)
    [('a', 'b'), ('1', '2')]
    

so that the elements are now grouped together by their index (the first ones
are all together, the second, etc.)

